+ rc = lbx_img_read_row_header(&img->pub, &x, &y);
+ if (rc == 0)
+ return 1;
+ else if (rc < 0)
+ return -1;
+
+ pos = &img->framedata[y][x];
+ rc = lbx_img_read_row_data(&img->pub, pos);
+ if (rc < 0)
+ return -1;
+ memset(&img->mask[y][x], 1, rc);
+
+ return 0;
+}
+
+static unsigned char **allocframebuffer(size_t width, size_t height)
+{
+ unsigned char **new, *tmp;
+ size_t i;
+
+ if (height > SIZE_MAX / sizeof *new) {
+ lbx_error_raise(LBX_ENOMEM);
+ return NULL;
+ }
+
+ /* Ensure that there is at least one row in the framebuffer. */
+ if (height == 0 || width == 0)
+ width = height = 1;
+
+ tmp = calloc(height, width);
+ if (!tmp) {
+ lbx_error_raise(LBX_ENOMEM);
+ return NULL;
+ }
+
+ new = malloc(height * sizeof *new);
+ if (!new) {
+ lbx_error_raise(LBX_ENOMEM);
+ free(tmp);
+ return NULL;
+ }
+
+ for (i = 0; i < height; i++) {
+ new[i] = tmp + i * width;
+ }
+
+ return new;
+}
+
+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;
+ }