X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/300cee673cd190b12dcb82641f3bab9a43cca6f2..6ddd77d5bb60d875253f2427d279ac6caacdce68:/src/lbximg.c
diff --git a/src/lbximg.c b/src/lbximg.c
index 921d42b..d52f688 100644
--- a/src/lbximg.c
+++ b/src/lbximg.c
@@ -1,22 +1,22 @@
-/* 2ooM: The Master of Orion II Reverse Engineering Project
- * Simple command-line tool to convert an LBX image to a set of PNGs.
- * Copyright (C) 2006-2008 Nick Bowler
+/*
+ * 2ooM: The Master of Orion II Reverse Engineering Project
+ * Simple command-line tool to convert an LBX image to a set of PNGs.
+ * Copyright (C) 2006-2010 Nick Bowler
*
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
*
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU General Public License for more details.
*
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ * You should have received a copy of the GNU General Public License
+ * along with this program. If not, see .
*/
-#define _GNU_SOURCE
+#include
#include
#include
#include
@@ -27,6 +27,7 @@
#include
+#include "tools.h"
#include "image.h"
#include "lbx.h"
@@ -35,6 +36,19 @@ static int verbose = 0;
static char *outname = "out";
static int usepalette = 1;
+static void printusage(void)
+{
+ puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
+ " [-f path]");
+ puts(" [frameno ...]");
+}
+
+static void printhelp(void)
+{
+ printusage();
+ puts("For now, see the man page for detailed help.");
+}
+
static const char *progname;
#define errmsg(fmt, ...) (\
fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
@@ -149,7 +163,6 @@ int outpng(unsigned int frameno,
}
if (setjmp(png_jmpbuf(png))) {
- free(row);
png_destroy_write_struct(&png, &info);
goto err;
}
@@ -162,12 +175,19 @@ int outpng(unsigned int frameno,
* data to libpng and let it do its magic.
*/
+ png_color png_palette[256];
+ for (unsigned i = 0; i < 256; i++) {
+ png_palette[i].red = palette[i].red;
+ png_palette[i].green = palette[i].green;
+ png_palette[i].blue = palette[i].blue;
+ }
+
png_set_IHDR(png, info, width, height, 8,
PNG_COLOR_TYPE_PALETTE, PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
- png_set_PLTE(png, info, (png_colorp)palette, 256);
+ png_set_PLTE(png, info, png_palette, 256);
png_set_rows(png, info, framedata);
png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
} else {
@@ -200,12 +220,11 @@ int outpng(unsigned int frameno,
png_destroy_write_struct(&png, &info);
fclose(of);
+ free(row);
if (verbose)
printf("wrote %s\n", name);
-
return 0;
-
err:
fclose(of);
remove(name);
@@ -215,7 +234,7 @@ err:
static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
{
- LBX_IMG *overimg = lbximg_fopen(f);
+ LBXimg *overimg = lbximg_open(f, &lbx_default_fops, NULL);
struct lbx_imginfo info;
if (!overimg) {
@@ -240,7 +259,7 @@ static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
return 0;
}
-static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
+static int loadpalette(LBXimg *img, struct lbx_imginfo *info,
FILE *palf, FILE *override,
struct lbx_colour palette[static 256])
{
@@ -267,7 +286,7 @@ static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
}
/* Read the external palette, if any. */
- if (palf && lbximg_loadpalette(palf, palette) == -1) {
+ if (palf && lbximg_loadpalette(palf, &lbx_default_fops, palette) != 0) {
errmsg("error reading external palette: %s\n", lbx_strerror());
return -1;
}
@@ -286,7 +305,7 @@ static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
return 0;
}
-int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
+int decode(LBXimg *img, FILE *palf, FILE *override, char **argv)
{
unsigned char *framebits;
struct lbx_colour palette[256];
@@ -296,7 +315,7 @@ int decode(LBX_IMG *img, FILE *palf, FILE *override, char **argv)
lbximg_getinfo(img, &info);
- framebits = malloc(info.nframes / CHAR_BIT + 1);
+ framebits = calloc(1, info.nframes / CHAR_BIT + 1);
if (!framebits) {
return EXIT_FAILURE;
}
@@ -350,22 +369,26 @@ err:
int main(int argc, char **argv)
{
- int mode = MODE_NONE;
- FILE *inf = stdin, *palf = NULL, *overf = NULL;
- const char *name = "stdin";
- LBX_IMG *img;
- int opt;
+ int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
+ struct lbx_pipe_state stdin_handle = { .f = stdin };
+ FILE *palf = NULL, *overf = NULL;
+ const char *file = NULL;
+ LBXimg *img;
- static const char *sopts = "idvf:p:O:";
+ static const char *sopts = "idnvf:p:O:V";
static const struct option lopts[] = {
- { "info", 0, NULL, 'i' },
- { "decode", 0, NULL, 'd' },
- { "verbose", 0, NULL, 'v' },
- { "file", 1, NULL, 'f' },
- { "palette", 1, NULL, 'p' },
- { "override", 1, NULL, 'p' },
+ { "ident", 0, NULL, 'i' },
+ { "decode", 0, NULL, 'd' },
+ { "verbose", 0, NULL, 'v' },
+ { "file", 1, NULL, 'f' },
+ { "palette", 1, NULL, 'p' },
+ { "override", 1, NULL, 'p' },
+
+ { "version", 0, NULL, 'V' },
+ { "usage", 0, NULL, 'U' },
+ { "help", 0, NULL, 'H' },
- { "nopalette", 0, &usepalette, 0 },
+ { "no-palette", 0, NULL, 'n' },
{ 0 }
};
@@ -383,17 +406,10 @@ int main(int argc, char **argv)
verbose = 1;
break;
case 'f':
- if (strcmp(optarg, "-") == 0)
- break;
-
- name = strrchr(optarg, '/');
- name = name ? name+1 : optarg;
-
- inf = fopen(optarg, "rb");
- if (!inf) {
- errmsg("failed to open %s: %m\n", optarg);
- return EXIT_FAILURE;
- }
+ file = optarg;
+ break;
+ case 'n':
+ usepalette = 0;
break;
case 'p':
palf = fopen(optarg, "rb");
@@ -410,6 +426,15 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
break;
+ case 'V':
+ puts(VERSION_BOILERPLATE("lbximg"));
+ return EXIT_SUCCESS;
+ case 'U':
+ printusage();
+ return EXIT_SUCCESS;
+ case 'H':
+ printhelp();
+ return EXIT_SUCCESS;
case '?':
case ':':
return EXIT_FAILURE;
@@ -421,7 +446,11 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}
- img = lbximg_fopen(inf);
+ if (file)
+ img = lbximg_fopen(file);
+ else
+ img = lbximg_open(&stdin_handle, &lbx_pipe_fops, NULL);
+
if (!img) {
errmsg("failed to open image: %s.\n", lbx_strerror());
return EXIT_FAILURE;
@@ -429,23 +458,24 @@ int main(int argc, char **argv)
if (verbose || mode == MODE_IDENT) {
struct lbx_imginfo info;
- lbximg_getinfo(img, &info);
+ if (!file)
+ file = "stdin";
+
+ lbximg_getinfo(img, &info);
printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
- name, info.width, info.height, info.nframes,
+ file, info.width, info.height, info.nframes,
info.palettesz ? ", embedded palette" : "",
+ info.chunk ? ", chunked" : "",
info.looping ? ", loops" : "");
}
switch (mode) {
case MODE_DECODE:
- if (decode(img, palf, overf, &argv[optind])) {
- lbximg_close(img);
- return EXIT_FAILURE;
- }
+ rc = decode(img, palf, overf, &argv[optind]);
break;
}
lbximg_close(img);
- return EXIT_SUCCESS;
+ return rc;
}