/* * 2ooM: The Master of Orion II Reverse Engineering Project * Simple command-line tool to convert an LBX image to a set of PNGs. * Copyright © 2006-2011, 2013 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 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. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ #include #include #include #include #include #include #include #include #include "tools.h" #include "image.h" #include "error.h" #include "lbx.h" #include "imgoutput.h" /* Global flags */ 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."); } enum { MODE_NONE, MODE_DECODE, MODE_IDENT, }; int parserange(unsigned frames, char *str, unsigned char *bits) { unsigned long start, end; unsigned int i; char *endptr; start = strtoul(str, &endptr, 0); if (start >= frames) { tool_err(-1, "frame %lu out of range.", start); return -1; } if (endptr == str) { tool_err(-1, "invalid frame range: %s.", str); return -1; } switch (*endptr) { case '\0': end = start; break; case '-': end = strtoul(endptr+1, &endptr, 0); if (end >= frames) { tool_err(-1, "frame %lu out of range.", end); return -1; } if (endptr == str) end = frames - 1; break; default: tool_err(-1, "invalid frame range: %s.", str); return -1; } if (end < start) { tool_err(-1, "invalid frame range: %s.", str); return -1; } for (i = start; i <= end; i++) { bits[i / CHAR_BIT] |= 1 << (i % CHAR_BIT); } return 0; } int outpng(unsigned int frameno, unsigned char **framedata, unsigned char **mask, unsigned int width, unsigned int height, struct lbx_colour palette[static 256]) { char name[strlen(outname) + sizeof ".65535.png"]; FILE *of; int rc; assert(frameno < 65536); snprintf(name, sizeof name, "%s.%03d.png", outname, frameno); of = fopen(name, "wb"); if (!of) { tool_err(0, "failed to open %s", name); return -1; } rc = img_output_png(of, name, width, height, framedata, mask, palette); if (rc < 0) { fclose(of); return -1; } if (fclose(of) == EOF) { tool_err(0, "error writing %s", name); return -1; } if (verbose) printf("wrote %s\n", name); return 0; } static int loadoverride(FILE *f, struct lbx_colour palette[static 256]) { struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL); struct lbx_imginfo info; if (!overimg) { tool_err(-1, "failed to open override image: %s", lbx_errmsg()); return -1; } lbx_img_getinfo(overimg, &info); if (!info.palettesz) { tool_err(-1, "override image has no palette."); lbx_img_close(overimg); return -1; } if (lbx_img_getpalette(overimg, palette) == -1) { tool_err(-1, "error reading override palette: %s", lbx_errmsg()); lbx_img_close(overimg); return -1; } lbx_img_close(overimg); return 0; } static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info, FILE *palf, FILE *override, struct lbx_colour palette[static 256]) { int i; /* For sanity. */ if (!palf && !info->palettesz && !override) { tool_err(-1, "no palette available."); return -1; } /* Default the palette to a wonderful pink. */ for (i = 0; i < 256; i++) { palette[i] = (struct lbx_colour){0xff, 0x00, 0xff}; } /* Read the external palette, if any. */ if (palf && lbx_img_loadpalette(palf, &lbx_default_fops, palette) != 0) { tool_err(-1, "error reading external palette: %s", lbx_errmsg()); return -1; } /* Read the embedded palette, if any. */ if (info->palettesz && lbx_img_getpalette(img, palette) == -1) { tool_err(-1, "error reading embedded palette: %s", lbx_errmsg()); return -1; } /* Read the override palette, if any. */ if (override && loadoverride(override, palette) == -1) { return -1; } return 0; } int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv) { unsigned char *framebits; struct lbx_colour palette[256]; struct lbx_imginfo info; int extracted = 0; unsigned int i; lbx_img_getinfo(img, &info); framebits = calloc(1, img->frames / CHAR_BIT + 1); if (!framebits) { return EXIT_FAILURE; } /* Figure out what images we're extracting. */ if (!argv[0]) { /* extract all images by default. */ memset(framebits, -1, img->frames / CHAR_BIT + 1); } else { for (i = 0; argv[i]; i++) { parserange(img->frames, argv[i], framebits); } } if (usepalette) { if (loadpalette(img, &info, palf, override, palette) == -1) { goto err; } } /* Extract the images, in order. */ for (i = 0; i < img->frames; i++) { unsigned char **data; unsigned char **mask; if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT)))) continue; data = lbx_img_getframe(img, i); if (!data) { tool_err(-1, "error in frame %u: %s", i, lbx_errmsg()); continue; } mask = lbx_img_getmask(img); if (!outpng(i, data, mask, img->width, img->height, usepalette ? palette : NULL)) { extracted = 1; } } if (!extracted) { tool_err(-1, "no frames extracted."); goto err; } free(framebits); return EXIT_SUCCESS; err: free(framebits); return EXIT_FAILURE; } int main(int argc, char **argv) { 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; struct lbx_image *img; static const char *sopts = "idnvf:p:O:V"; static const struct option lopts[] = { { "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' }, { "no-palette", 0, NULL, 'n' }, { 0 } }; tool_init("lbximg", argc, argv); while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) { switch(opt) { case 'i': mode = MODE_IDENT; break; case 'd': mode = MODE_DECODE; break; case 'v': verbose = 1; break; case 'f': file = optarg; break; case 'n': usepalette = 0; break; case 'p': palf = fopen(optarg, "rb"); if (!palf) { tool_err(0, "failed to open %s", optarg); return EXIT_FAILURE; } break; case 'O': overf = fopen(optarg, "rb"); if (!overf) { tool_err(0, "failed to open %s", optarg); return EXIT_FAILURE; } break; case 'V': tool_version(); return EXIT_SUCCESS; case 'U': printusage(); return EXIT_SUCCESS; case 'H': printhelp(); return EXIT_SUCCESS; case '?': case ':': return EXIT_FAILURE; } } if (mode == MODE_NONE) { tool_err(-1, "you must specify a mode."); return EXIT_FAILURE; } if (file) img = lbx_img_fopen(file); else img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL); if (!img) { tool_err(-1, "failed to open image: %s.", lbx_errmsg()); return EXIT_FAILURE; } if (verbose || mode == MODE_IDENT) { struct lbx_imginfo info; if (!file) file = "stdin"; lbx_img_getinfo(img, &info); printf("%s is %ux%u LBX image, %u frame(s)%s%s\n", file, img->width, img->height, img->frames, info.palettesz ? ", embedded palette" : "", img->chunk ? ", chunked" : "", info.looping ? ", loops" : ""); } switch (mode) { case MODE_DECODE: rc = decode(img, palf, overf, &argv[optind]); break; } lbx_img_close(img); return rc; }