]> git.draconx.ca Git - liblbx.git/blobdiff - src/image.c
liblbx: Kill byteorder.h.
[liblbx.git] / src / image.c
index f32a66094372eda5de7418afa32450abad9ab2f2..888515b45293ad22b5815f4be9a8885fe3136f5e 100644 (file)
@@ -1,3 +1,21 @@
+/*
+ *  2ooM: The Master of Orion II Reverse Engineering Project
+ *  Library for working with LBX image files.
+ *  Copyright (C) 2006-2008 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
+ *  the Free Software Foundation, either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License
+ *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
@@ -8,7 +26,7 @@
 #include <assert.h>
 #include <errno.h>
 
-#include "byteorder.h"
+#include "pack.h"
 #include "misc.h"
 #include "lbx.h"
 #include "image.h"
 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
 #define FLAG_PALETTE   0x1000 /* Image contains embedded palette. */
 #define FLAG_LOOPING   0x2000 /* Loop over all frames in the image (unsure). */
+#define FLAG_ALL (FLAG_OVERWRITE|FLAG_PALETTE|FLAG_LOOPING)
+
+#define HDR_LEN 12
 
 struct lbx_image {
-       uint16_t width, height;
-       uint16_t wtf1;
-       uint16_t frames, leadin;
-       uint16_t flags;
+       unsigned short width, height;
+       unsigned short wtf, flags;
+       unsigned short frames, leadin;
+       unsigned short palstart, palcount;
 
        FILE *f;
-       long foff;
+       long foff, paloff;
 
        int currentframe;
        int currentx, currenty;
        unsigned char **framedata;
        unsigned char **mask;
 
-       uint32_t offsets[];
+       unsigned long offsets[];
 };
 
+static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
+{
+       unsigned short nframes = unpack_16_le(hdr+6);
+       struct lbx_image *img;
+
+       img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
+       if (!img) {
+               lbx_errno = -errno;
+               return NULL;
+       }
+
+       *img = (struct lbx_image) {
+               .width  = unpack_16_le(hdr+0),
+               .height = unpack_16_le(hdr+2),
+               .wtf    = unpack_16_le(hdr+4),
+               .frames = unpack_16_le(hdr+6),
+               .leadin = unpack_16_le(hdr+8),
+               .flags  = unpack_16_le(hdr+10),
+
+               .currentframe = -1,
+       };
+
+       return img;
+}
+
 struct lbx_image *lbximg_fopen(FILE *f)
 {
-       struct lbx_image tmp = {.f = f}, *new = NULL;
+       unsigned char hdr_buf[HDR_LEN];
+       struct lbx_image *img;
        size_t rc;
 
-       if (fread(&tmp.width,  sizeof tmp.width,   1, f) != 1) goto readerr;
-       if (fread(&tmp.height, sizeof tmp.height,  1, f) != 1) goto readerr;
-       if (fread(&tmp.wtf1,   sizeof tmp.wtf1,    1, f) != 1) goto readerr;
-       if (fread(&tmp.frames, sizeof tmp.frames,  1, f) != 1) goto readerr;
-       if (fread(&tmp.leadin, sizeof tmp.leadin,  1, f) != 1) goto readerr;
-       if (fread(&tmp.flags,  sizeof tmp.flags,   1, f) != 1) goto readerr;
-
-       tmp.width  = letohs(tmp.width);  tmp.foff += sizeof tmp.width;
-       tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
-       tmp.wtf1   = letohs(tmp.wtf1);   tmp.foff += sizeof tmp.wtf1;
-       tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
-       tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
-       tmp.flags  = letohs(tmp.flags);  tmp.foff += sizeof tmp.flags;
-
-       /* Format constraints. */
-       if (tmp.frames <= tmp.leadin) {
-               lbx_errno = LBX_EFORMAT;
+       if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
+               lbx_errno = -errno;
+               if (feof(f))
+                       lbx_errno = LBX_EEOF;
                return NULL;
        }
 
+       img = lbximg_init(hdr_buf);
+       if (!img)
+               return NULL;
+
+       img->f    = f;
+       img->foff = sizeof hdr_buf;
+
        /*
         * DEBUG ONLY.  These assertions exist to catch otherwise valid image
         * files which differ from what I believe to be true of all LBX images.
         * If we never find any exceptions, we can replace the assertions with
         * assumptions.
         */
-       _lbx_assert(tmp.wtf1 == 0);
-       _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
+       _lbx_assert(img->wtf == 0); /* version? */
+       _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
+       _lbx_assert(!(img->flags & ~FLAG_ALL));
+
+       /* Read all offsets.  Should be merged with identical code in lbx.c */
+       for (unsigned i = 0; i <= img->frames; i++) {
+               unsigned char buf[4];
+
+               if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
+                       lbx_errno = -errno;
+                       if (feof(f))
+                               lbx_errno = LBX_EEOF;
+                       free(img);
+                       return NULL;
+               }
 
-       new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
-       if (!new) {
-               lbx_errno = -errno;
-               return NULL;
+               img->offsets[i] = unpack_32_le(buf);
+               img->foff += 4;
        }
 
-       *new = tmp;
-       new->currentframe = -1;
+       if (img->flags & FLAG_PALETTE) {
+               unsigned char buf[4];
 
-       rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
-       if (rc < new->frames+1)
-               goto readerr;
-       new->foff += sizeof *new->offsets * (new->frames+1);
+               if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
+                       lbx_errno = -errno;
+                       if (feof(f))
+                               lbx_errno = LBX_EEOF;
+                       free(img);
+                       return NULL;
+               }
 
-       return new;
-readerr:
-       if (feof(f)) {
-               lbx_errno = LBX_EEOF;
-       } else {
-               lbx_errno = -errno;
+               img->palstart = unpack_16_le(buf+0);
+               img->palcount = unpack_16_le(buf+2);
+               img->foff    += sizeof buf;
+               img->paloff   = img->foff;
+
+               if (img->palstart + img->palcount > 256) {
+                       lbx_errno = LBX_EFORMAT;
+                       free(img);
+                       return NULL;
+               }
        }
 
-       free(new);
-       return NULL;
+       return img;
 }
 
 static int _lbx_drawrow(int first, struct lbx_image *img)
 {
-       uint16_t type, yval, count, xval;
+       unsigned short type, count, yval, xval;
+       unsigned char buf[4];
        unsigned char *pos;
-       unsigned char abyss;
        size_t rc;
 
        assert(img->framedata);
        assert(img->mask);
 
-       if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
-       type = letohs(type); img->foff += sizeof type;
+       if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
+               goto readerr;
+       img->foff += 4;
+       type = unpack_16_le(buf+0);
 
        if (first) {
                img->currentx = 0;
@@ -114,14 +174,16 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
        }
 
        if (type == 0) {
-               if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
-               yval = letohs(yval); img->foff += sizeof yval;
+               yval = unpack_16_le(buf+2);
                if (yval == 1000)
                        return 1;
-               if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
-               count = letohs(count); img->foff += sizeof count;
-               if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
-               xval = letohs(xval); img->foff += sizeof xval;
+
+               if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
+                       goto readerr;
+               img->foff += 4;
+               count = unpack_16_le(buf+0);
+
+               xval = unpack_16_le(buf+2);
                if (xval == 1000)
                        return 1;
 
@@ -134,8 +196,7 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
                img->currenty += yval;
                img->currentx  = xval;
        } else {
-               if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
-               xval = letohs(xval); img->foff += sizeof xval;
+               xval = unpack_16_le(buf+2);
 
                if (img->width - img->currentx <= xval) {
                        lbx_errno = LBX_EFORMAT;
@@ -144,7 +205,6 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
                img->currentx += xval;
 
                count = type;
-
        }
 
        if (count > img->width - img->currentx) {
@@ -163,7 +223,7 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
                goto readerr;
 
        if (count % 2) {
-               if (fread(&abyss, 1, 1, img->f) != 1)
+               if (fread(buf, 1, 1, img->f) != 1)
                        goto readerr;
                img->foff += 1;
        }
@@ -290,38 +350,19 @@ int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
 int
 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
 {
-       size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
        unsigned int i;
        size_t rc;
 
-       uint16_t start, count;
        uint8_t  entry[4];
 
        /* Do nothing if the image doesn't have embedded palette data. */
        if (!(img->flags & FLAG_PALETTE))
                return 0;
 
-       /* Palette data is located right after the header. */
-       if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
-               return -1;
-       
-       /* Palette header */
-       if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
-       if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
-       start = letohs(start); img->foff += sizeof start;
-       count = letohs(count); img->foff += sizeof count;
-
-       if (start + count > 256) {
-               lbx_errno = LBX_EFORMAT;
+       if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
                return -1;
-       }
-
-       if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
-               lbx_errno = LBX_EFORMAT;
-               return -1;
-       }
 
-       for (i = 0; i < count; i++) {
+       for (i = 0; i < img->palcount; i++) {
                rc = fread(entry, 1, sizeof entry, img->f);
                img->foff += rc;
 
@@ -334,7 +375,7 @@ lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
                        return -1;
                }
 
-               palette[start + i] = (struct lbx_colour){
+               palette[img->palstart + i] = (struct lbx_colour){
                        .red   = entry[1] << 2,
                        .green = entry[2] << 2,
                        .blue  = entry[3] << 2,
@@ -353,7 +394,7 @@ void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
                .width      = img->width,
                .height     = img->height,
                .nframes    = img->frames,
-               .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
+               .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
        };
 
        /* There seems to be two ways of specifying that an image loops. */