]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbximg.c
license: Fix copyright years in source files.
[liblbx.git] / src / lbximg.c
index 67d8635a8feaf4ba64d75cf1d89d4c026772e243..a8a5df3cb81cc7008551bb7aa317e03e12ca1eea 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
+ *  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
@@ -163,7 +163,6 @@ int outpng(unsigned int frameno,
        }
 
        if (setjmp(png_jmpbuf(png))) {
-               free(row);
                png_destroy_write_struct(&png, &info);
                goto err;
        }
@@ -221,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);
@@ -236,7 +234,7 @@ err:
 
 static int loadoverride(FILE *f, struct lbx_colour palette[static 256])
 {
-       LBX_IMG *overimg = lbximg_fopen(f);
+       LBX_IMG *overimg = lbximg_open(f, &lbx_default_fops, NULL);
        struct lbx_imginfo info;
 
        if (!overimg) {
@@ -317,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;
        }
@@ -372,25 +370,25 @@ err:
 int main(int argc, char **argv)
 {
        int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
-       struct lbx_pipe_state state = { .f = stdin };
+       struct lbx_pipe_state stdin_handle = { .f = stdin };
        FILE *palf = NULL, *overf = NULL;
-       const char *name = "stdin";
+       const char *file = NULL;
        LBX_IMG *img;
 
-       static const char *sopts = "idvf:p:O:V";
+       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' },
+               { "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' },
+               { "version",    0, NULL, 'V' },
+               { "usage",      0, NULL, 'U' },
+               { "help",       0, NULL, 'H' },
 
-               { "nopalette", 0, &usepalette, 0 },
+               { "no-palette", 0, NULL, 'n' },
 
                { 0 }
        };
@@ -408,13 +406,10 @@ int main(int argc, char **argv)
                        verbose = 1;
                        break;
                case 'f':
-                       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");
@@ -451,10 +446,10 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       if (fseek(state.f, 0, SEEK_CUR) == 0)
-               img = lbximg_open(state.f, &lbx_default_fops, NULL);
+       if (file)
+               img = lbximg_fopen(file);
        else
-               img = lbximg_open(&state, &lbx_pipe_fops, NULL);
+               img = lbximg_open(&stdin_handle, &lbx_pipe_fops, NULL);
 
        if (!img) {
                errmsg("failed to open image: %s.\n", lbx_strerror());
@@ -463,11 +458,15 @@ 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" : "");
        }