- struct lbx_image tmp = {.f = f}, *new = NULL;
-
- 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.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.offs = letohs(tmp.offs); tmp.foff += sizeof tmp.offs;
- tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
- tmp.wtf2 = letohs(tmp.wtf2); tmp.foff += sizeof tmp.wtf2;
-
- /* For some reason, the format seems to need this. */
- tmp.offs++;
- tmp.frames++;
-
- if (tmp.offs <= tmp.frames) {
- lbx_errno = LBX_EFORMAT;
+ 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_open(void *f, const struct lbx_file_ops *fops,
+ int (*destructor)(void *))
+{
+ unsigned char hdr_buf[HDR_LEN];
+ struct lbx_image *img;
+
+ if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
+ lbx_errno = -errno;
+ if (fops->eof(f))
+ lbx_errno = LBX_EEOF;