]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbximg.c
Trivial manual fixes.
[liblbx.git] / src / lbximg.c
index 7827a87751685e487201c5590d679193700dece8..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 a set of PNGs.
- *  Copyright (C) 2006-2008 Nick Bowler
+ *  Simple command-line tool to convert an LBX image to other formats.
+ *  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
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <string.h>
 #include <limits.h>
+#include <stdint.h>
 #include <assert.h>
 #include <getopt.h>
 #include <errno.h>
 
-#include <png.h>
+#include "help.h"
 
 #include "tools.h"
 #include "image.h"
+#include "error.h"
 #include "lbx.h"
 
+#include "imgoutput.h"
+
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+
 /* Global flags */
 static int verbose = 0;
 static char *outname = "out";
 static int usepalette = 1;
 
-static void printusage(void)
+#include "imgopts.h"
+static const char sopts[] = SOPT_STRING;
+static const struct option lopts[] = { LOPTS_INITIALIZER, {0} };
+
+static void print_usage(FILE *f)
 {
-       puts("usage: lbximg [-i|-d] [-v] [-p palette_file] [-O override_file]"
-                         " [-f path]");
-       puts("              [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;
 
-static const char *progname;
-#define errmsg(fmt, ...) (\
-       fprintf(stderr, "%s: " fmt, progname, __VA_ARGS__)\
-)
+       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 {
        MODE_NONE,
@@ -60,20 +87,76 @@ enum {
        MODE_IDENT,
 };
 
-int parserange(struct lbx_imginfo *info, char *str, unsigned char *bits)
+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 long npixels = (unsigned long) width * height;
+       unsigned long mask_sz = npixels / CHAR_BIT + (npixels % CHAR_BIT != 0);
+
+       for (unsigned long i = 0; i < mask_sz; i++) {
+               if (i+1 < mask_sz) {
+                       if (mask[i] != (unsigned char)-1)
+                               return true;
+               } else {
+                       unsigned char test = (1u << npixels % CHAR_BIT) - 1;
+
+                       if ((mask[i] & test) != test)
+                               return true;
+               }
+       }
+
+       return false;
+}
+
+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 >= info->nframes) {
-               errmsg("frame %lu out of range.\n", start);
+       if (start >= frames) {
+               tool_err(-1, "frame %lu out of range.", start);
                return -1;
        }
 
        if (endptr == str) {
-               errmsg("invalid frame range: %s.\n", str);
+               tool_err(-1, "invalid frame range: %s.", str);
                return -1;
        }
 
@@ -83,21 +166,21 @@ int parserange(struct lbx_imginfo *info, char *str, unsigned char *bits)
                break;
        case '-':
                end = strtoul(endptr+1, &endptr, 0);
-               if (end >= info->nframes) {
-                       errmsg("frame %lu out of range.\n", end);
+               if (end >= frames) {
+                       tool_err(-1, "frame %lu out of range.", end);
                        return -1;
                }
 
                if (endptr == str)
-                       end = info->nframes - 1;
+                       end = frames - 1;
                break;
        default:
-               errmsg("invalid frame range: %s.\n", str);
+               tool_err(-1, "invalid frame range: %s.", str);
                return -1;
        }
 
        if (end < start) {
-               errmsg("invalid frame range: %s.\n", str);
+               tool_err(-1, "invalid frame range: %s.", str);
                return -1;
        }
 
@@ -108,287 +191,281 @@ int parserange(struct lbx_imginfo *info, 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,
-           unsigned char **framedata, unsigned char **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"];
-       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) {
-               errmsg("failed to allocate row buffer: %s\n", strerror(errno));
-               return -1;
-       }
+       snprintf(name, sizeof name, "%s.%03d.%s", outname, frameno, fmt->name);
 
        of = fopen(name, "wb");
        if (!of) {
-               errmsg("failed to open %s: %s.\n", name, strerror(errno));
-               free(row);
+               tool_err(0, "failed to open %s", name);
                return -1;
        }
 
-       png = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
-       if (!png) {
-               errmsg("failed to init libpng.\n", 0);
-               goto err;
+       rc = fmt->output(of, name, width, height, pixels, pixel_mask, palette);
+       if (rc < 0) {
+               fclose(of);
+               return -1;
        }
 
-       info = png_create_info_struct(png);
-       if (!info) {
-               errmsg("failed to init libpng.\n", 0);
-               png_destroy_write_struct(&png, NULL);
-               goto err;
+       if (fclose(of) == EOF) {
+               tool_err(0, "error writing %s", name);
+               return -1;
        }
 
-       if (setjmp(png_jmpbuf(png))) {
-               free(row);
-               png_destroy_write_struct(&png, &info);
-               goto err;
-       }
+       if (verbose)
+               printf("wrote %s\n", name);
 
-       png_init_io(png, of);
+       return 0;
+}
 
-       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.
-                */
+static int loadoverride(FILE *f, struct lbx_colour *palette)
+{
+       struct lbx_image *img;
+       int rc, ret = 0;
 
-               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_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);
+       img = lbx_img_open(f, &lbx_default_fops, NULL);
+       if (!img) {
+               tool_err(-1, "failed to open override image: %s", lbx_errmsg());
+               return -1;
        }
 
-       png_destroy_write_struct(&png, &info);
-       fclose(of);
-
-       if (verbose)
-               printf("wrote %s\n", name);
-
-       return 0;
+       rc = lbx_img_getpalette(img, palette);
+       if (rc < 0) {
+               tool_err(-1, "error reading override palette: %s", lbx_errmsg());
+               ret = -1;
+       } else if (rc == 0) {
+               tool_err(-1, "override image has no palette.");
+               ret = -1;
+       }
 
-err:
-       fclose(of);
-       remove(name);
-       free(row);
-       return -1;
+       lbx_img_close(img);
+       return ret;
 }
 
-static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
+static int loadpalette(struct lbx_image *img, FILE *palf, FILE *override,
+                       struct lbx_colour *palette)
 {
-       LBX_IMG *overimg = lbximg_fopen(f);
-       struct lbx_imginfo info;
+       int rc, ret = -1;
 
-       if (!overimg) {
-               errmsg("failed to open override image: %s\n", lbx_strerror());
-               return -1;
+       /* Default the palette to a wonderful pink. */
+       for (unsigned i = 0; i < 256; i++) {
+               palette[i] = (struct lbx_colour){0x3f, 0x00, 0x3f};
        }
-       lbximg_getinfo(overimg, &info);
 
-       if (!info.palettesz) {
-               errmsg("override image has no palette.\n", 0);
-               lbximg_close(overimg);
-               return -1;
+       /* Read the external palette, if any. */
+       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;
        }
 
-       if (lbximg_getpalette(overimg, palette) == -1) {
-               errmsg("error reading override palette: %s\n", lbx_strerror());
-               lbximg_close(overimg);
+       /* 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;
        }
 
-       lbximg_close(overimg);
-       return 0;
+       /* Read the override palette, if any. */
+       if (override) {
+               rc = loadoverride(override, palette);
+               if (rc < 0)
+                       return -1;
+               ret = 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;
 }
 
-static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
-                       FILE *palf, FILE *override,
-                       struct lbx_colour palette[static 256])
+/* Return true iff a divides b. */
+static bool divides(unsigned a, unsigned b)
 {
-       int i;
+       if (b == 0)
+               return true;
+       if (a == 0)
+               return false;
 
-       /* 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 b % a == 0;
+}
 
-               return 0;
-       }
+/* Set n bits starting from offset in the bitmap. */
+static void
+set_bits(unsigned char *bitmap, unsigned long offset, unsigned long n)
+{
+       if (offset % CHAR_BIT) {
+               bitmap[offset/CHAR_BIT] |= (n >= CHAR_BIT ? -1u : (1u << n) - 1)
+                                 << offset%CHAR_BIT;
 
-       /* For sanity. */
-       if (!palf && !info->palettesz && !override) {
-               errmsg("no palette available.\n", 0);
-               return -1;
+               n -= MIN(n, CHAR_BIT - offset%CHAR_BIT);
+               offset += CHAR_BIT - offset%CHAR_BIT;
        }
 
-       /* Default the palette to a wonderful pink. */
-       for (i = 0; i < 256; i++) {
-               palette[i] = (struct lbx_colour){0xff, 0x00, 0xff};
+       if (n > CHAR_BIT) {
+               memset(&bitmap[offset/CHAR_BIT], -1, n/CHAR_BIT);
+
+               offset += n - n%CHAR_BIT;
+               n %= CHAR_BIT;
        }
 
-       /* Read the external palette, if any. */
-       if (palf && lbximg_loadpalette(palf, palette) == -1) {
-               errmsg("error reading external palette: %s\n", lbx_strerror());
-               return -1;
+       if (n) {
+               bitmap[offset/CHAR_BIT] |= (1u << n) - 1;
        }
+}
+
+static int decode_frame(struct lbx_image *img, unsigned n,
+                        unsigned char *pixels, unsigned char *pixel_mask)
+{
+       unsigned x, y;
+       long rc;
 
-       /* Read the embedded palette, if any. */
-       if (info->palettesz && lbximg_getpalette(img, palette) == -1) {
-               errmsg("error reading embedded palette: %s\n", lbx_strerror());
+       rc = lbx_img_seek(img, n);
+       if (rc < 0) {
+               tool_err(-1, "frame %u: invalid frame: %s\n", n, lbx_errmsg());
                return -1;
        }
 
-       /* Read the override palette, if any. */
-       if (override && loadoverride(override, palette) == -1) {
-               return -1;
+       while ((rc = lbx_img_read_row_header(img, &x, &y)) != 0) {
+               unsigned long offset;
+
+               if (rc < 0) {
+                       tool_err(-1, "frame %u: invalid row: %s", n, lbx_errmsg());
+                       return -1;
+               }
+
+               offset = (unsigned long) y * img->width + x;
+               rc = lbx_img_read_row_data(img, pixels+offset);
+               if (rc < 0) {
+                       tool_err(-1, "frame %u: error reading row: %s\n", n, lbx_errmsg());
+                       return -1;
+               }
+
+               set_bits(pixel_mask, offset, rc);
        }
 
        return 0;
 }
 
-int decode(LBX_IMG *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;
+       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;
+       size_t npixels, mask_sz;
 
-       lbximg_getinfo(img, &info);
+       assert(fmt >= 0 && fmt < sizeof formats / sizeof formats[0]);
 
-       framebits = malloc(info.nframes / CHAR_BIT + 1);
+       npixels = img->width;
+       if (img->height && npixels >= (size_t)-1 / img->height) {
+               tool_err(-1, "image too large");
+               goto err;
+       }
+       npixels *= img->height;
+
+       /* Ensure there is at least 1 byte to allocate */
+       if (npixels == 0)
+               npixels = 1;
+
+       framebits = calloc(1, img->frames / CHAR_BIT + 1);
        if (!framebits) {
-               return EXIT_FAILURE;
+               tool_err(0, "failed to allocate memory");
+               goto err;
+       }
+
+       pixels = calloc(img->width, img->height);
+       if (!pixels) {
+               tool_err(0, "failed to allocate memory");
+               goto err;
+       }
+
+       mask_sz = npixels / CHAR_BIT + (npixels % CHAR_BIT != 0);
+       pixel_mask = malloc(mask_sz);
+       if (!pixel_mask) {
+               tool_err(0, "failed to allocate memory");
+               goto err;
        }
 
        /* Figure out what images we're extracting. */
        if (!argv[0]) {
                /* extract all images by default. */
-               memset(framebits, -1, info.nframes / CHAR_BIT + 1);
+               memset(framebits, -1, img->frames / CHAR_BIT + 1);
        } else {
                for (i = 0; argv[i]; i++) {
-                       parserange(&info, argv[i], framebits);
+                       parserange(img->frames, argv[i], framebits);
                }
        }
 
-       if (loadpalette(img, &info, palf, override, palette) == -1) {
-               goto err;
+       if (usepalette) {
+               if (loadpalette(img, palf, override, palette) == -1) {
+                       ret = EXIT_FAILURE;
+                       goto err;
+               }
        }
 
        /* Extract the images, in order. */
-       for (i = 0; i < info.nframes; i++) {
-               unsigned char **data;
-               unsigned char **mask;
-
-               if (!(framebits[i / CHAR_BIT] & (1 << (i % CHAR_BIT))))
-                       continue;
-
-               data = lbximg_getframe(img, i);
-               if (!data) {
-                       errmsg("error in frame %u: %s\n", i, lbx_strerror());
-                       continue;
+       ret = EXIT_SUCCESS;
+       for (i = 0; i < img->frames; i++) {
+               if (divides(img->chunk, i))
+                       memset(pixel_mask, 0, mask_sz);
+
+               rc = decode_frame(img, i, pixels, pixel_mask);
+               if (rc < 0) {
+                       ret = EXIT_FAILURE;
+                       goto err;
                }
 
-               mask = lbximg_getmask(img);
+               if (framebits[i / CHAR_BIT] & (1u << (i % CHAR_BIT))) {
+                       rc = output(i, &formats[fmt], pixels, pixel_mask,
+                                   img->width, img->height,
+                                   usepalette ? palette : NULL);
 
-               if (!outpng(i, data, mask, info.width, info.height, palette)) {
-                       extracted = 1;
+                       if (rc == 0) {
+                               extracted = 1;
+                       }
                }
        }
 
        if (!extracted) {
-               errmsg("no frames extracted.\n", 0);
-               goto err;
+               tool_err(-1, "no frames extracted.");
+               ret = EXIT_FAILURE;
        }
-
-       free(framebits);
-       return EXIT_SUCCESS;
 err:
+       free(pixels);
+       free(pixel_mask);
        free(framebits);
-       return EXIT_FAILURE;
+       return ret;
 }
 
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
-       struct lbx_pipe_state state = { .f = stdin };
+       int mode = MODE_NONE, fmt, opt, rc = EXIT_FAILURE;
+       struct lbx_pipe_state stdin_handle = { .f = stdin };
+       const char *file = NULL, *fmtstring = NULL;
+       const char *ext_palette = NULL, *ovr_palette = NULL;
        FILE *palf = NULL, *overf = NULL;
-       const char *name = "stdin";
-       LBX_IMG *img;
-
-       static const char *sopts = "idvf: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' },
-
-               { "nopalette", 0, &usepalette, 0 },
-
-               { 0 }
-       };
+       struct lbx_image *img;
 
-       progname = "lbximg"; /* argv[0]; */
+       tool_init("lbximg", argc, argv);
        while ((opt = getopt_long(argc, argv, sopts, lopts, NULL)) != -1) {
                switch(opt) {
                case 'i':
@@ -400,79 +477,97 @@ int main(int argc, char **argv)
                case 'v':
                        verbose = 1;
                        break;
+               case 'F':
+                       fmtstring = optarg;
+                       break;
                case 'f':
-                       if (strcmp(optarg, "-") == 0)
-                               break;
-
-                       name = strrchr(optarg, '/');
-                       name = name ? name+1 : optarg;
-
-                       if (!freopen(optarg, "rb", state.f)) {
-                               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");
-                       if (!palf) {
-                               errmsg("failed to open %s: %m\n", optarg);
-                               return EXIT_FAILURE;
-                       }
+                       ext_palette = optarg;
 
                        break;
                case 'O':
-                       overf = fopen(optarg, "rb");
-                       if (!overf) {
-                               errmsg("failed to open %s: %m\n", optarg);
-                               return EXIT_FAILURE;
-                       }
+                       ovr_palette = optarg;
+                       break;
+               case LOPT_OUTPUT_PREFIX:
+                       outname = optarg;
                        break;
                case 'V':
-                       puts(VERSION_BOILERPLATE("lbximg"));
-                       return EXIT_SUCCESS;
-               case 'U':
-                       printusage();
+                       tool_version();
                        return EXIT_SUCCESS;
                case 'H':
-                       printhelp();
+                       print_help();
                        return EXIT_SUCCESS;
-               case '?':
-               case ':':
+               default:
                        return EXIT_FAILURE;
                }
        }
 
        if (mode == MODE_NONE) {
-               errmsg("you must specify a mode.\n", 0);
+               tool_err(-1, "you must specify a mode.");
                return EXIT_FAILURE;
        }
 
-       if (fseek(state.f, 0, SEEK_CUR) == 0)
-               img = lbximg_open(state.f, &lbx_default_fops, NULL);
+       fmt = lookup_format(fmtstring);
+       if (fmt < 0)
+               return EXIT_FAILURE;
+
+       if (file)
+               img = lbx_img_fopen(file);
        else
-               img = lbximg_open(&state, &lbx_pipe_fops, NULL);
+               img = lbx_img_open(&stdin_handle, &lbx_pipe_fops, NULL);
 
        if (!img) {
-               errmsg("failed to open image: %s.\n", lbx_strerror());
+               tool_err(-1, "failed to open image: %s.", lbx_errmsg());
+               return EXIT_FAILURE;
+       }
+
+       if (ext_palette && !(palf = fopen(ext_palette, "rb"))) {
+               tool_err(0, "failed to open %s", optarg);
+               return EXIT_FAILURE;
+       }
+
+       if (ovr_palette && !(overf = fopen(ovr_palette, "rb"))) {
+               tool_err(0, "failed to open %s", optarg);
                return EXIT_FAILURE;
        }
 
        if (verbose || mode == MODE_IDENT) {
-               struct lbx_imginfo info;
-               lbximg_getinfo(img, &info);
+               int palette_count;
+
+               if (!file)
+                       file = "stdin";
 
-               printf("%s is %ux%u LBX image, %u frame(s)%s%s\n",
-                      name, info.width, info.height, info.nframes,
-                      info.palettesz ? ", embedded palette" : "",
-                      info.looping   ? ", loops" : "");
+               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,
+                      palette_count ? ", embedded palette" : "",
+                      img->chunk    ? ", chunked" : "",
+                      img->leadin+1 < img->frames ? ", loops" : "");
        }
 
        switch (mode) {
+       case MODE_IDENT:
+               rc = 0;
+               break;
        case MODE_DECODE:
-               rc = decode(img, palf, overf, &argv[optind]);
+               rc = decode(img, palf, overf, fmt, &argv[optind]);
                break;
        }
 
-       lbximg_close(img);
+       lbx_img_close(img);
+       if (palf)
+               fclose(palf);
+       if (overf)
+               fclose(overf);
        return rc;
 }