- 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;
+ 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;