]> git.draconx.ca Git - liblbx.git/blobdiff - src/lbximg.c
liblbx: Update lbximg_fopen to work like lbx_fopen.
[liblbx.git] / src / lbximg.c
index 50dff078f7050588c595c60ad179a52785942706..1173538856b94ae4781a78aedd86795d18719433 100644 (file)
@@ -16,7 +16,6 @@
  *  You should have received a copy of the GNU General Public License
  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
-#define _GNU_SOURCE
 #include <config.h>
 #include <stdio.h>
 #include <stdlib.h>
@@ -164,7 +163,6 @@ int outpng(unsigned int frameno,
        }
 
        if (setjmp(png_jmpbuf(png))) {
-               free(row);
                png_destroy_write_struct(&png, &info);
                goto err;
        }
@@ -177,12 +175,19 @@ int outpng(unsigned int frameno,
                 * 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_colorp)palette, 256);
+               png_set_PLTE(png, info, png_palette, 256);
                png_set_rows(png, info, framedata);
                png_write_png(png, info, PNG_TRANSFORM_IDENTITY, NULL);
        } else {
@@ -215,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);
@@ -230,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) {
@@ -282,7 +286,7 @@ static int loadpalette(LBX_IMG *img, struct lbx_imginfo *info,
        }
 
        /* Read the external palette, if any. */
-       if (palf && lbximg_loadpalette(palf, palette) == -1) {
+       if (palf && lbximg_loadpalette(palf, &lbx_default_fops, palette) != 0) {
                errmsg("error reading external palette: %s\n", lbx_strerror());
                return -1;
        }
@@ -311,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;
        }
@@ -365,11 +369,11 @@ err:
 
 int main(int argc, char **argv)
 {
-       int mode = MODE_NONE;
-       FILE *inf = stdin, *palf = NULL, *overf = NULL;
+       int mode = MODE_NONE, opt, rc = EXIT_FAILURE;
+       struct lbx_pipe_state state = { .f = stdin };
+       FILE *palf = NULL, *overf = NULL;
        const char *name = "stdin";
        LBX_IMG *img;
-       int opt;
 
        static const char *sopts = "idvf:p:O:V";
        static const struct option lopts[] = {
@@ -402,14 +406,10 @@ int main(int argc, char **argv)
                        verbose = 1;
                        break;
                case 'f':
-                       if (strcmp(optarg, "-") == 0)
-                               break;
-
                        name = strrchr(optarg, '/');
                        name = name ? name+1 : optarg;
 
-                       inf = fopen(optarg, "rb");
-                       if (!inf) {
+                       if (!freopen(optarg, "rb", state.f)) {
                                errmsg("failed to open %s: %m\n", optarg);
                                return EXIT_FAILURE;
                        }
@@ -449,7 +449,11 @@ int main(int argc, char **argv)
                return EXIT_FAILURE;
        }
 
-       img = lbximg_fopen(inf);
+       if (fseek(state.f, 0, SEEK_CUR) == 0)
+               img = lbximg_open(state.f, &lbx_default_fops, NULL);
+       else
+               img = lbximg_open(&state, &lbx_pipe_fops, NULL);
+
        if (!img) {
                errmsg("failed to open image: %s.\n", lbx_strerror());
                return EXIT_FAILURE;
@@ -462,18 +466,16 @@ int main(int argc, char **argv)
                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.chunk     ? ", chunked" : "",
                       info.looping   ? ", loops" : "");
        }
 
        switch (mode) {
        case MODE_DECODE:
-               if (decode(img, palf, overf, &argv[optind])) {
-                       lbximg_close(img);
-                       return EXIT_FAILURE;
-               }
+               rc = decode(img, palf, overf, &argv[optind]);
                break;
        }
 
        lbximg_close(img);
-       return EXIT_SUCCESS;
+       return rc;
 }