]> git.draconx.ca Git - liblbx.git/blobdiff - src/image.c
liblbx: Kill byteorder.h.
[liblbx.git] / src / image.c
index 304386140ed2a3d7e273b920dd0d90ea46c78a9a..888515b45293ad22b5815f4be9a8885fe3136f5e 100644 (file)
@@ -26,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;
-       uint16_t palstart, palcount;
+       unsigned short width, height;
+       unsigned short wtf, flags;
+       unsigned short frames, leadin;
+       unsigned short palstart, palcount;
 
        FILE *f;
        long foff, paloff;
@@ -50,27 +52,53 @@ struct lbx_image {
        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;
+       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;
 
-       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;
+       img->f    = f;
+       img->foff = sizeof hdr_buf;
 
        /*
         * DEBUG ONLY.  These assertions exist to catch otherwise valid image
@@ -78,66 +106,66 @@ struct lbx_image *lbximg_fopen(FILE *f)
         * If we never find any exceptions, we can replace the assertions with
         * assumptions.
         */
-       _lbx_assert(tmp.wtf1 == 0);
-       _lbx_assert(tmp.frames > tmp.leadin); /* cmbtshp.lbx breaks this. */
-       _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;
-
-       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 (img->flags & FLAG_PALETTE) {
+               unsigned char buf[4];
 
-       if (new->flags & FLAG_PALETTE) {
-               if (fread(&new->palstart, sizeof new->palstart, 1, f) != 1)
-                       goto readerr;
-               if (fread(&new->palcount, sizeof new->palcount, 1, f) != 1)
-                       goto readerr;
+               if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
+                       lbx_errno = -errno;
+                       if (feof(f))
+                               lbx_errno = LBX_EEOF;
+                       free(img);
+                       return NULL;
+               }
 
-               new->palstart = letohs(new->palstart);
-               new->palcount = letohs(new->palcount);
-               new->foff    += sizeof new->palstart + sizeof new->palcount;
-               new->paloff   = new->foff;
+               img->palstart = unpack_16_le(buf+0);
+               img->palcount = unpack_16_le(buf+2);
+               img->foff    += sizeof buf;
+               img->paloff   = img->foff;
 
-               if (new->palstart + new->palcount > 256) {
+               if (img->palstart + img->palcount > 256) {
                        lbx_errno = LBX_EFORMAT;
-                       free(new);
+                       free(img);
                        return NULL;
                }
        }
 
-       return new;
-readerr:
-       if (feof(f)) {
-               lbx_errno = LBX_EEOF;
-       } else {
-               lbx_errno = -errno;
-       }
-
-       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;
@@ -146,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;
 
@@ -166,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;
@@ -176,7 +205,6 @@ static int _lbx_drawrow(int first, struct lbx_image *img)
                img->currentx += xval;
 
                count = type;
-
        }
 
        if (count > img->width - img->currentx) {
@@ -195,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;
        }
@@ -333,7 +361,7 @@ lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
 
        if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
                return -1;
-       
+
        for (i = 0; i < img->palcount; i++) {
                rc = fread(entry, 1, sizeof entry, img->f);
                img->foff += rc;