+int lbx_img_seek(struct lbx_image *pub, unsigned frame)
+{
+ struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
+
+ if (frame >= pub->frames) {
+ lbx_error_raise(LBX_EINVAL);
+ return -1;
+ }
+
+ if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
+ return -1;
+ }
+
+ if (!(img->flags & FLAG_RAW)) {
+ unsigned char buf[4];
+
+ /* Read frame header */
+ if (img->fops->read(buf, 4, img->f) != 4) {
+ if (img->fops->eof(img->f))
+ lbx_error_raise(LBX_EEOF);
+ return -1;
+ }
+
+ if (unpack_16_le(buf) != 1) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+
+ img->currentx = 0;
+ img->currenty = unpack_16_le(buf+2);
+ if (img->currenty > img->pub.height) {
+ lbx_error_raise(LBX_EFORMAT);
+ return -1;
+ }
+ }
+
+ img->read_state = READ_STATE_HEADER;
+ return 0;
+}
+
+long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
+{
+ struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
+ unsigned short length, offset;
+ unsigned char buf[4];
+
+ if (img->read_state != READ_STATE_HEADER) {
+ lbx_error_raise(LBX_EINVAL);
+ return -1;
+ }
+
+ /* Raw images have no row headers */
+ if (img->flags & FLAG_RAW) {
+ img->currentn = img->pub.width;
+ *y = img->currenty++;
+ *x = 0;
+
+ if (*y < img->pub.height) {
+ img->read_state = READ_STATE_DATA;
+ return img->currentn;
+ } else {
+ img->read_state = READ_STATE_DONE;
+ return 0;
+ }
+ }
+
+ 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 unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
+{
+ unsigned long size = img->pub.width * img->pub.height;
+ int rc;
+
+ assert(img->flags & FLAG_RAW);
+
+ rc = lbx_img_seek(&img->pub, frame);
+ if (rc < 0)
+ return NULL;
+
+ if (img->fops->read(img->framedata[0], size, img->f) != size) {
+ if (img->fops->eof(img->f))
+ lbx_error_raise(LBX_EEOF);
+ return NULL;
+ }
+ memset(img->mask[0], 1, size);
+
+ if (img->fops->tell(img->f) > img->offsets[frame+1]) {
+ lbx_error_raise(LBX_EFORMAT);
+ return NULL;
+ }
+
+ return img->framedata;
+}
+
+unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)