]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbximg.c
Trivial manual fixes.
[liblbx.git] / src / lbximg.c
index dc4697b31568c0f6cb7483f7fca3aabdb4b5ab25..f9975063ff20804d6f0ccbeef6b6f4ce1070e265 100644 (file)
@@ -1,7 +1,7 @@
 /*
  *  2ooM: The Master of Orion II Reverse Engineering Project
  *  Simple command-line tool to convert an LBX image to other formats.
- *  Copyright © 2006-2011, 2013-2014 Nick Bowler
+ *  Copyright © 2006-2011, 2013-2014, 2021 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
@@ -27,6 +27,8 @@
 #include <getopt.h>
 #include <errno.h>
 
+#include "help.h"
+
 #include "tools.h"
 #include "image.h"
 #include "error.h"
@@ -41,21 +43,42 @@ static int verbose = 0;
 static char *outname = "out";
 static int usepalette = 1;
 
-enum {
-       OPT_PREFIX = UCHAR_MAX+1,
-};
+#include "imgopts.h"
+static const char sopts[] = SOPT_STRING;
+static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
 
-static void printusage(void)
+static void print_usage(FILE *f)
 {
-       puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
-                         " [-f path]");
-       puts("              [-F format] [frameno ...]");
+       const char *progname = tool_invocation();
+
+       fprintf(f, "Usage: %s [options] [-i|-d] [frame ...]\n", progname);
+       if (f != stdout)
+               fprintf(f, "Try %s --help for more information.\n", progname);
 }
 
-static void printhelp(void)
+static void print_help(void)
 {
-       printusage();
-       puts("For now, see the man page for detailed help.");
+       const struct option *opt;
+
+       print_usage(stdout);
+
+       putchar('\n');
+       puts("Options:");
+       for (opt = lopts; opt->name; opt++) {
+               struct lopt_help help;
+               int w;
+
+               if (!lopt_get_help(opt, &help))
+                       continue;
+
+               help_print_option(opt, help.arg, help.desc, 20);
+       }
+       putchar('\n');
+
+       puts("For more information, see the lbximg(1) man page.");
+       putchar('\n');
+
+       printf("Report bugs to <%s>.\n", PACKAGE_BUGREPORT);
 }
 
 enum {
@@ -168,10 +191,10 @@ int parserange(unsigned frames, char *str, unsigned char *bits)
        return 0;
 }
 
-int output(unsigned int frameno, const struct img_format *fmt,
-           unsigned char *pixels, unsigned char *pixel_mask,
-           unsigned int width, unsigned int height,
-           struct lbx_colour palette[static 256])
+static int output(unsigned int frameno, const struct img_format *fmt,
+                  unsigned char *pixels, unsigned char *pixel_mask,
+                  unsigned int width, unsigned int height,
+                  struct lbx_colour *palette)
 {
        char name[strlen(outname) + sizeof ".65535.png"];
        FILE *of;
@@ -204,68 +227,72 @@ int output(unsigned int frameno, const struct img_format *fmt,
        return 0;
 }
 
-static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
+static int loadoverride(FILE *f, struct lbx_colour *palette)
 {
-       struct lbx_image *overimg = lbx_img_open(f, &lbx_default_fops, NULL);
-       struct lbx_imginfo info;
+       struct lbx_image *img;
+       int rc, ret = 0;
 
-       if (!overimg) {
+       img = lbx_img_open(f, &lbx_default_fops, NULL);
+       if (!img) {
                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) {
+       rc = lbx_img_getpalette(img, palette);
+       if (rc < 0) {
                tool_err(-1, "error reading override palette: %s", lbx_errmsg());
-               lbx_img_close(overimg);
-               return -1;
+               ret = -1;
+       } else if (rc == 0) {
+               tool_err(-1, "override image has no palette.");
+               ret = -1;
        }
 
-       lbx_img_close(overimg);
-       return 0;
+       lbx_img_close(img);
+       return ret;
 }
 
-static int loadpalette(struct lbx_image *img, struct lbx_imginfo *info,
-                       FILE *palf, FILE *override,
-                       struct lbx_colour palette[static 256])
+static int loadpalette(struct lbx_image *img, FILE *palf, FILE *override,
+                       struct lbx_colour *palette)
 {
-       int i;
-
-       /* For sanity. */
-       if (!palf && !info->palettesz && !override) {
-               tool_err(-1, "no palette available.");
-               return -1;
-       }
+       int rc, ret = -1;
 
        /* Default the palette to a wonderful pink. */
-       for (i = 0; i < 256; i++) {
+       for (unsigned i = 0; i < 256; i++) {
                palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
        }
 
        /* 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;
+       if (palf) {
+               rc = lbx_img_loadpalette(palf, &lbx_default_fops, palette);
+               if (rc < 0) {
+                       tool_err(-1, "error reading external palette: %s", lbx_errmsg());
+                       return -1;
+               }
+
+               ret = 0;
        }
 
-       /* Read the embedded palette, if any. */
-       if (info->palettesz && lbx_img_getpalette(img, palette) == -1) {
+       /* Read the embedded palette */
+       rc = lbx_img_getpalette(img, palette);
+       if (rc < 0) {
                tool_err(-1, "error reading embedded palette: %s", lbx_errmsg());
                return -1;
+       } else if (rc > 0) {
+               ret = 0;
        }
 
        /* Read the override palette, if any. */
-       if (override && loadoverride(override, palette) == -1) {
-               return -1;
+       if (override) {
+               rc = loadoverride(override, palette);
+               if (rc < 0)
+                       return -1;
+               ret = 0;
        }
 
-       return 0;
+       /* If we literally have no palette data at all, may as well fail. */
+       if (ret < 0)
+               tool_err(-1, "no palette available.");
+       return ret;
 }
 
 /* Return true iff a divides b. */
@@ -341,7 +368,6 @@ decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
 {
        unsigned char *pixels = NULL, *pixel_mask = NULL, *framebits = NULL;
        struct lbx_colour palette[256];
-       struct lbx_imginfo info;
        int rc, ret = EXIT_FAILURE;
        int extracted = 0;
        unsigned int i;
@@ -349,10 +375,8 @@ decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
 
        assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]);
 
-       lbx_img_getinfo(img, &info);
-
        npixels = img->width;
-       if (img->height && npixels >= SIZE_MAX / img->height) {
+       if (img->height && npixels >= (size_t)-1 / img->height) {
                tool_err(-1, "image too large");
                goto err;
        }
@@ -392,7 +416,7 @@ decode(struct lbx_image *img, FILE *palf, FILE *override, int fmt, char **argv)
        }
 
        if (usepalette) {
-               if (loadpalette(img, &info, palf, override, palette) == -1) {
+               if (loadpalette(img, palf, override, palette) == -1) {
                        ret = EXIT_FAILURE;
                        goto err;
                }
@@ -441,26 +465,6 @@ int main(int argc, char **argv)
        FILE *palf = NULL, *overf = NULL;
        struct lbx_image *img;
 
-       static const char sopts[] = "idnvF:f:p:O:VH";
-       static const struct option lopts[] = {
-               { "identify",      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, 'O' },
-               { "no-palette",    0, NULL, 'n' },
-
-               { "output-prefix", 1, NULL,  OPT_PREFIX },
-
-               { "version",       0, NULL, 'V' },
-               { "usage",         0, NULL, 'U' },
-               { "help",          0, NULL, 'H' },
-
-               { 0 }
-       };
-
        tool_init("lbximg", argc, argv);
        while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch(opt) {
@@ -489,17 +493,14 @@ int main(int argc, char **argv)
                case 'O':
                        ovr_palette = optarg;
                        break;
-               case OPT_PREFIX:
+               case LOPT_OUTPUT_PREFIX:
                        outname = optarg;
                        break;
                case 'V':
                        tool_version();
                        return EXIT_SUCCESS;
-               case 'U':
-                       printusage();
-                       return EXIT_SUCCESS;
                case 'H':
-                       printhelp();
+                       print_help();
                        return EXIT_SUCCESS;
                default:
                        return EXIT_FAILURE;
@@ -536,17 +537,22 @@ int main(int argc, char **argv)
        }
 
        if (verbose || mode == MODE_IDENT) {
-               struct lbx_imginfo info;
+               int palette_count;
 
                if (!file)
                        file = "stdin";
 
-               lbx_img_getinfo(img, &info);
+               palette_count = lbx_img_getpalette(img, NULL);
+               if (palette_count < 0) {
+                       tool_err(-1, "error reading image: %s", lbx_errmsg());
+                       return EXIT_FAILURE;
+               }
+
                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" : "",
-                      info.looping   ? ", loops" : "");
+                      palette_count ? ", embedded palette" : "",
+                      img->chunk    ? ", chunked" : "",
+                      img->leadin+1 < img->frames ? ", loops" : "");
        }
 
        switch (mode) {