+ do {
+ if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
+ if (img->fops->eof(img->f))
+ lbx_error_raise(LBX_EEOF);
+ return -1;
+ }
+
+ length = unpack_16_le(buf+0);
+ offset = unpack_16_le(buf+2);
+
+ if (length == 0) {
+ if (offset == 1000) {
+ img->read_state = READ_STATE_DONE;
+ return 0;
+ } else if (offset > img->pub.height - img->currenty) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+
+ img->currenty += offset;
+ img->currentx = 0;
+ }
+ } while (length == 0);
+
+ if (offset > img->pub.width - img->currentx) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+ img->currentx += offset;
+
+ if (length > img->pub.width - img->currentx) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+ img->currentn = length;
+
+ img->read_state = READ_STATE_DATA;
+ *x = img->currentx;
+ *y = img->currenty;
+
+ return img->currentn;
+}
+
+long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
+{
+ struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
+
+ if (img->read_state != READ_STATE_DATA) {
+ lbx_error_raise(LBX_EINVAL);
+ return -1;
+ }
+
+ if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
+ if (img->fops->eof(img->f))
+ lbx_error_raise(LBX_EEOF);
+ return -1;
+ }
+
+ if (!(img->flags & FLAG_RAW)) {
+ /* Skip padding byte, if any */
+ if (img->currentn % 2) {
+ if (img->fops->seek(img->f, 1, SEEK_CUR))
+ return -1;
+ }
+ }
+
+ img->read_state = READ_STATE_HEADER;
+ img->currentx += img->currentn;
+
+ return img->currentn;
+}
+
+static int read_palette(void *f, const struct lbx_file_ops *fops,
+ struct lbx_colour *palette, unsigned count,
+ bool external)
+{
+ assert(count <= 256);
+ for (unsigned i = 0; i < count; i++) {
+ unsigned char buf[4];
+
+ if (fops->read(buf, 4, f) != 4) {
+ if (fops->eof(f))
+ lbx_error_raise(LBX_EEOF);
+ return -1;
+ }
+
+ if (buf[0] != external) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+
+ palette[i] = (struct lbx_colour) {
+ .red = buf[1] & 0x3f,
+ .green = buf[2] & 0x3f,
+ .blue = buf[3] & 0x3f,
+ .active = 1,
+ };
+ }
+
+ return 0;
+}
+
+int lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
+ struct lbx_colour *palette)
+{
+ return read_palette(f, fops, palette, 256, true);
+}
+
+int lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour *out)
+{
+ struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
+ unsigned long palette_start, palette_count, palette_offset;
+ unsigned char buf[4];
+ int rc;
+
+ /* Do nothing if the image doesn't have embedded palette data. */
+ if (!(img->flags & FLAG_PALETTE))
+ return 0;
+
+ palette_offset = 16 + 4ul * img->pub.frames;
+ if (img->fops->seek(img->f, palette_offset, SEEK_SET)) {
+ return -1;
+ }
+
+ /* Read embedded palette header */
+ if (img->fops->read(buf, 4, img->f) < 4)
+ goto readerr;
+
+ palette_start = unpack_16_le(buf+0);
+ palette_count = unpack_16_le(buf+2);
+ if (palette_start + palette_count > 256) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+
+ if (out) {
+ rc = read_palette(img->f, img->fops,
+ out+palette_start, palette_count,
+ false);
+ if (rc < 0)
+ return -1;
+ }
+
+ return palette_count;
+readerr:
+ if (img->fops->eof(img->f))
+ lbx_error_raise(LBX_EEOF);
+ return -1;
+}
+
+int lbx_img_close(struct lbx_image *pub)
+{
+ struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
+ int rc = 0;
+
+ if (img && img->dtor) {
+ rc = img->dtor(img->f);
+ }