]> git.draconx.ca Git - liblbx.git/commitdiff
Start adding image support to liblbx.
authorNick Bowler <draconx@gmail.com>
Sat, 29 Dec 2007 19:28:31 +0000 (14:28 -0500)
committerNick Bowler <draconx@gmail.com>
Sat, 29 Dec 2007 19:28:31 +0000 (14:28 -0500)
src/Makefile.am
src/image.c [new file with mode: 0644]
src/image.h [new file with mode: 0644]

index d1ddaf2dc2e88bc5da00147da07e0c561e4a1023..6867d306aa4185f4bc64207ba7e76c6232babdc0 100644 (file)
@@ -2,7 +2,7 @@ lib_LTLIBRARIES   = liblbx.la
 bin_PROGRAMS      = lbxtool
 include_HEADERS   = lbx.h
 
-liblbx_la_SOURCES = byteorder.h lbx.c
+liblbx_la_SOURCES = byteorder.h lbx.c image.c
 
 lbxtool_SOURCES   = lbxtool.c
 lbxtool_LDADD     = liblbx.la
diff --git a/src/image.c b/src/image.c
new file mode 100644 (file)
index 0000000..86f6f98
--- /dev/null
@@ -0,0 +1,80 @@
+#ifdef HAVE_CONFIG_H
+#      include "config.h"
+#endif
+
+#include <stdlib.h>
+#include <stdint.h>
+#include <assert.h>
+#include <errno.h>
+
+#include "lbx.h"
+#include "byteorder.h"
+
+struct lbx_image {
+       FILE *f;
+       uint16_t width, height;
+       uint16_t wtf1, wtf2;
+       uint16_t offs, frames;
+       uint32_t offsets[];
+};
+
+struct lbx_image *lbximg_fopen(FILE *f)
+{
+       struct lbx_image tmp = {.f = f}, *new;
+
+       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.offs,   sizeof tmp.offs,    1, f) != 1) goto readerr;
+       if (fread(&tmp.frames, sizeof tmp.frames,  1, f) != 1) goto readerr;
+       if (fread(&tmp.wtf2,   sizeof tmp.wtf2,    1, f) != 1) goto readerr;
+
+       tmp.width  = letohs(tmp.width);
+       tmp.height = letohs(tmp.height);
+       tmp.wtf1   = letohs(tmp.wtf1);
+       tmp.offs   = letohs(tmp.offs);
+       tmp.frames = letohs(tmp.frames);
+       tmp.wtf2   = letohs(tmp.wtf2);
+
+       /*
+        * 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.
+        */
+       assert(tmp.wtf1 == 0);
+       assert(tmp.offs == tmp.frames + 1);
+
+       new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
+       if (!new) {
+               lbx_errno = -errno;
+               return NULL;
+       }
+
+       *new = tmp;
+
+       if (fread(new->offsets, sizeof *new->offsets, new->offs, f) != new->offs)
+               goto readerr;
+
+       return new;
+readerr:
+       if (feof(f)) {
+               lbx_errno = LBX_EEOF;
+       } else {
+               lbx_errno = -errno;
+       }
+
+       free(new);
+       return NULL;
+}
+
+void lbximg_close(struct lbx_image *img)
+{
+       if (!img) return;
+
+       if (img->f) {
+               fclose(img->f);
+       }
+
+       free(img);
+}
diff --git a/src/image.h b/src/image.h
new file mode 100644 (file)
index 0000000..5b5e4a3
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef LBXIMG_H_
+#define LBXIMG_H_
+
+#include <stdio.h>
+
+typedef struct lbx_image LBXIMG;
+
+LBXIMG *lbximg_fopen(FILE *f);
+void    lbximg_close(LBXIMG *img);
+
+#endif