]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbximg.c
lbximg: Fix --override long option.
[liblbx.git] / src / lbximg.c
index ff38c89b71f998f0dff387f5f2404e4ffb0783cf..ca618ca20b88dac70204f9f61c9006c95f19e339 100644 (file)
@@ -1,6 +1,6 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
- *  Simple command-line tool to convert an LBX image to a set of PNGs.
+ *  Simple command-line tool to convert an LBX image to other formats.
  *  Copyright © 2006-2011, 2013 Nick Bowler
  *
  *  This program is free software: you can redistribute it and/or modify
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <limits.h>
 #include <assert.h>
 #include <getopt.h>
 #include <errno.h>
 
-#include <png.h>
-
 #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";
@@ -41,7 +42,7 @@ static void printusage(void)
 {
        puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
                          " [-f path]");
-       puts("              [frameno ...]");
+       puts("              [-F format] [frameno ...]");
 }
 
 static void printhelp(void)
@@ -56,6 +57,54 @@ enum {
        MODE_IDENT,
 };
 
+static const struct img_format {
+       img_output_func *output;
+       char name[4];
+       bool enabled;
+} formats[] = {
+#if HAVE_LIBPNG
+       { img_output_png, "png", 1 },
+#endif
+       { img_output_pam, "pam", 1 },
+       { img_output_ppm, "ppm", 1 },
+       { img_output_pbm, "pbm", 1 },
+};
+
+static int lookup_format(const char *fmt)
+{
+       for (size_t i = 0; i < sizeof formats / sizeof formats[0]; i++) {
+               assert(!formats[i].name[sizeof formats[i].name - 1]);
+
+               if (!fmt && formats[i].enabled)
+                       return i;
+
+               if (strcmp(formats[i].name, fmt))
+                       continue;
+
+               if (!formats[i].enabled) {
+                       tool_err(-1, "%s support disabled at build time", fmt);
+                       return -1;
+               }
+
+               return i;
+       }
+
+       tool_err(-1, "unknown format %s", fmt);
+       return -1;
+}
+
+bool img_is_masked(unsigned char **mask, unsigned width, unsigned height)
+{
+       unsigned x, y;
+
+       for (x = y = 0; y < height; ++x < width || (x = 0, y++)) {
+               if (mask[y][x] == 0)
+                       return true;
+       }
+
+       return false;
+}
+
 int parserange(unsigned frames, char *str, unsigned char *bits)
 {
        unsigned long start, end;
@@ -104,39 +153,18 @@ int parserange(unsigned frames, char *str, unsigned char *bits)
        return 0;
 }
 
-static int ismasked(unsigned char **mask, unsigned width, unsigned height)
-{
-       unsigned y, x;
-       for (y = 0; y < height; y++) {
-               for (x = 0; x < width; x++) {
-                       if (mask[y][x] == 0) return 1;
-               }
-       }
-
-       return 0;
-}
-
-int outpng(unsigned int frameno,
+int output(unsigned int frameno, const struct img_format *fmt,
            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"];
-       unsigned char *row;
-       unsigned int x, y;
        FILE *of;
+       int rc;
 
-       png_structp png;
-       png_infop   info;
-
+       assert(fmt->output != NULL);
        assert(frameno < 65536);
-       snprintf(name, sizeof name, "%s.%03d.png", outname, frameno);
-
-       row = malloc(4 * width);
-       if (!row) {
-               tool_err(0, "failed to allocate row buffer");
-               return -1;
-       }
+       snprintf(name, sizeof name, "%s.%03d.%s", outname, frameno, fmt->name);
 
        of = fopen(name, "wb");
        if (!of) {
@@ -144,87 +172,21 @@ int outpng(unsigned int frameno,
                return -1;
        }
 
-       png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-       if (!png) {
-               tool_err(-1, "failed to init libpng.");
-               goto err;
-       }
-
-       info = png_create_info_struct(png);
-       if (!info) {
-               tool_err(-1, "failed to init libpng.");
-               png_destroy_write_struct(&png, NULL);
-               goto err;
+       rc = fmt->output(of, name, width, height, framedata, mask, palette);
+       if (rc < 0) {
+               fclose(of);
+               return -1;
        }
 
-       if (setjmp(png_jmpbuf(png))) {
-               png_destroy_write_struct(&png, &info);
-               goto err;
+       if (fclose(of) == EOF) {
+               tool_err(0, "error writing %s", name);
+               return -1;
        }
-
-       png_init_io(png, of);
-
-       if (!ismasked(mask, width, height)) {
-               /*
-                * This case is easy; we can just feed the palette and pixel
-                * 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_palette, 256);
-               png_set_rows(png, info, framedata);
-               png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
-       } else {
-               /*
-                * Unfortunately, LBX doesn't translate nicely to PNG here.
-                * LBX has a 256 colour palette _plus_ transparency.
-                * We'll form an RGBA PNG to deal with this.
-                */
-
-               png_set_IHDR(png, info, width, height, 8,
-                            PNG_COLOR_TYPE_RGB_ALPHA, PNG_INTERLACE_NONE,
-                            PNG_COMPRESSION_TYPE_DEFAULT,
-                            PNG_FILTER_TYPE_DEFAULT);
-       
-               png_write_info(png, info);
-       
-               for (y = 0; y < height; y++) {
-                       for (x = 0; x < width; x++) {
-                               row[4*x+0] = palette[framedata[y][x]].red;
-                               row[4*x+1] = palette[framedata[y][x]].green;
-                               row[4*x+2] = palette[framedata[y][x]].blue;
-                               row[4*x+3] = (mask[y][x]) ? -1 : 0;
-                       }
-       
-                       png_write_row(png, row);
-               }
-       
-               png_write_end(png, NULL);
-       }
-
-       png_destroy_write_struct(&png, &info);
-       fclose(of);
-       free(row);
-
        if (verbose)
                printf("wrote %s\n", name);
+
        return 0;
-err:
-       fclose(of);
-       remove(name);
-       free(row);
-       return -1;
 }
 
 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
@@ -260,15 +222,6 @@ static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
 {
        int i;
 
-       /* In no-palette mode, use palette indices for colour. */
-       if (!usepalette) {
-               for (i = 0; i < 256; i++) {
-                       palette[i] = (struct lbx_colour){i,i,i};
-               }
-
-               return 0;
-       }
-
        /* For sanity. */
        if (!palf && !info->palettesz && !override) {
                tool_err(-1, "no palette available.");
@@ -277,7 +230,7 @@ static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
 
        /* Default the palette to a wonderful pink. */
        for (i = 0; i < 256; i++) {
-               palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
+               palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
        }
 
        /* Read the external palette, if any. */
@@ -300,7 +253,8 @@ static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
        return 0;
 }
 
-int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
+static int
+decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
 {
        unsigned char *framebits;
        struct lbx_colour palette[256];
@@ -308,6 +262,8 @@ int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
        int extracted = 0;
        unsigned int i;
 
+       assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]);
+
        lbx_img_getinfo(img, &info);
 
        framebits = calloc(1, img->frames / CHAR_BIT + 1);
@@ -325,8 +281,10 @@ int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
                }
        }
 
-       if (loadpalette(img, &info, palf, override, palette) == -1) {
-               goto err;
+       if (usepalette) {
+               if (loadpalette(img, &info, palf, override, palette) == -1) {
+                       goto err;
+               }
        }
 
        /* Extract the images, in order. */
@@ -345,7 +303,9 @@ int decode(struct lbx_image *img, FILE *palf, FILE *override, char **argv)
 
                mask = lbx_img_getmask(img);
 
-               if (!outpng(i, data, mask, img->width, img->height, palette)) {
+               if (!output(i, &formats[fmt], data, mask,
+                           img->width, img->height,
+                           usepalette ? palette : NULL)) {
                        extracted = 1;
                }
        }
@@ -364,20 +324,21 @@ err:
 
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
+       int mode = MODE_NONE, fmt, opt, rc = EXIT_FAILURE;
        struct lbx_pipe_state stdin_handle = { .f = stdin };
+       const char *file = NULL, *fmtstring = NULL;
        FILE *palf = NULL, *overf = NULL;
-       const char *file = NULL;
        struct lbx_image *img;
 
-       static const char *sopts = "idnvf:p:O:V";
+       static const char sopts[] = "idnvF:f:p:O:VH";
        static const struct option lopts[] = {
                { "ident",      0, NULL, 'i' },
                { "decode",     0, NULL, 'd' },
                { "verbose",    0, NULL, 'v' },
                { "file",       1, NULL, 'f' },
+               { "format",     1, NULL, 'F' },
                { "palette",    1, NULL, 'p' },
-               { "override",   1, NULL, 'p' },
+               { "override",   1, NULL, 'O' },
 
                { "version",    0, NULL, 'V' },
                { "usage",      0, NULL, 'U' },
@@ -400,6 +361,9 @@ int main(int argc, char **argv)
                case 'v':
                        verbose = 1;
                        break;
+               case 'F':
+                       fmtstring = optarg;
+                       break;
                case 'f':
                        file = optarg;
                        break;
@@ -430,8 +394,7 @@ int main(int argc, char **argv)
                case 'H':
                        printhelp();
                        return EXIT_SUCCESS;
-               case '?':
-               case ':':
+               default:
                        return EXIT_FAILURE;
                }
        }
@@ -441,6 +404,10 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
+       fmt = lookup_format(fmtstring);
+       if (fmt < 0)
+               return EXIT_FAILURE;
+
        if (file)
                img = lbx_img_fopen(file);
        else
@@ -458,7 +425,7 @@ int main(int argc, char **argv)
                        file = "stdin";
 
                lbx_img_getinfo(img, &info);
-               printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
+               printf("%s is %hux%hu LBX image, %hhu frame(s)%s%s%s\n",
                       file, img->width, img->height, img->frames,
                       info.palettesz ? ", embedded palette" : "",
                       img->chunk     ? ", chunked" : "",
@@ -467,7 +434,7 @@ int main(int argc, char **argv)
 
        switch (mode) {
        case MODE_DECODE:
-               rc = decode(img, palf, overf, &argv[optind]);
+               rc = decode(img, palf, overf, fmt, &argv[optind]);
                break;
        }