X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/blobdiff_plain/2ec44cef64979d8e19eb66206c26a7bcad4bef8a..9f64a8229b5299c6815bef7b9f4180f9232ed52e:/src/image.c?ds=sidebyside diff --git a/src/image.c b/src/image.c index b30f165..786e17a 100644 --- a/src/image.c +++ b/src/image.c @@ -39,6 +39,14 @@ #define HDR_LEN 12 +/* States for image readout */ +enum { + READ_STATE_INIT, + READ_STATE_HEADER, + READ_STATE_DATA, + READ_STATE_DONE, +}; + struct lbx_image_priv { struct lbx_image pub; @@ -52,10 +60,9 @@ struct lbx_image_priv { long paloff; - int currentframe; - int currentx, currenty; - unsigned char **framedata; - unsigned char **mask; + /* State of frame readout */ + unsigned currentx, currenty, currentn; + int read_state; unsigned long offsets[]; }; @@ -80,8 +87,6 @@ static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN]) .pub.leadin = hdr[8], .pub.chunk = hdr[9], .flags = unpack_16_le(hdr+10), - - .currentframe = -1, }; if (img->flags & FLAG_OVERWRITE) @@ -202,214 +207,142 @@ struct lbx_image *lbx_img_fopen(const char *file) return lbx_img_open(p, &lbx_pipe_fops, pipe_close); } -static int _lbx_drawrow(struct lbx_image_priv *img) +int lbx_img_seek(struct lbx_image *pub, unsigned frame) { - unsigned short length, offset; - unsigned char buf[4]; - unsigned char *pos; - size_t rc; - - assert(img->framedata); - assert(img->mask); - - if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) - goto readerr; - - length = unpack_16_le(buf+0); - offset = unpack_16_le(buf+2); - if (length == 0 && offset == 1000) - return 1; - - /* Length of 0 increments Y position */ - if (!length) { - if (offset > img->pub.height - img->currenty) { - lbx_error_raise(LBX_EFORMAT); - return -1; - } - - img->currenty += offset; - img->currentx = 0; - return 0; - } + struct lbx_image_priv *img = (struct lbx_image_priv *)pub; - /* Otherwise we read pixel data */ - if (offset > img->pub.width - img->currentx) { - lbx_error_raise(LBX_EFORMAT); + if (frame >= pub->frames) { + lbx_error_raise(LBX_EINVAL); return -1; } - img->currentx += offset; - if (length > img->pub.width - img->currentx) { - lbx_error_raise(LBX_EFORMAT); + if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) { return -1; } - memset(&img->mask[img->currenty][img->currentx], 1, length); + if (!(img->flags & FLAG_RAW)) { + unsigned char buf[4]; - pos = &img->framedata[img->currenty][img->currentx]; - rc = img->fops->read(pos, length, img->f); - img->currentx += rc; + /* 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 (rc < length) - goto readerr; + if (unpack_16_le(buf) != 1) { + lbx_error_raise(LBX_EFORMAT); + return -1; + } - /* Skip padding byte, if any */ - if (length % 2) { - if (img->fops->read(buf, 1, img->f) != 1) - goto readerr; + 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; -readerr: - if (img->fops->eof(img->f)) - lbx_error_raise(LBX_EEOF); - return -1; } -static unsigned char **allocframebuffer(size_t width, size_t height) +long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y) { - 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; - } + struct lbx_image_priv *img = (struct lbx_image_priv *)pub; + unsigned short length, offset; + unsigned char buf[4]; - new = malloc(height * sizeof *new); - if (!new) { - lbx_error_raise(LBX_ENOMEM); - free(tmp); - return NULL; + if (img->read_state != READ_STATE_HEADER) { + lbx_error_raise(LBX_EINVAL); + return -1; } - for (i = 0; i < height; i++) { - new[i] = tmp + i * width; + /* 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; + } } - return new; -} + 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; + } -static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame) -{ - unsigned long size = img->pub.width * img->pub.height; + length = unpack_16_le(buf+0); + offset = unpack_16_le(buf+2); - assert(img->flags & FLAG_RAW); + 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; + } - if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) { - return NULL; - } + img->currenty += offset; + img->currentx = 0; + } + } while (length == 0); - if (img->fops->read(img->framedata[0], size, img->f) != size) { - if (img->fops->eof(img->f)) - lbx_error_raise(LBX_EEOF); - return NULL; + if (offset > img->pub.width - img->currentx) { + lbx_error_raise(LBX_EFORMAT); + return -1; } - memset(img->mask[0], 1, size); + img->currentx += offset; - if (img->fops->tell(img->f) > img->offsets[frame+1]) { + if (length > img->pub.width - img->currentx) { lbx_error_raise(LBX_EFORMAT); - return NULL; + return -1; } + img->currentn = length; - return img->framedata; + img->read_state = READ_STATE_DATA; + *x = img->currentx; + *y = img->currenty; + + return img->currentn; } -unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame) +long lbx_img_read_row_data(struct lbx_image *pub, void *buf) { struct lbx_image_priv *img = (struct lbx_image_priv *)pub; - unsigned char buf[4]; - if (frame >= pub->frames || frame < 0) { + if (img->read_state != READ_STATE_DATA) { lbx_error_raise(LBX_EINVAL); - return NULL; - } - - if (!img->framedata) { - img->framedata = allocframebuffer(pub->width, pub->height); - if (!img->framedata) - return NULL; + return -1; } - if (!img->mask) { - img->mask = allocframebuffer(pub->width, pub->height); - if (!img->mask) - return NULL; + 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) - return read_raw_frame(img, frame); - - if ((img->flags & FLAG_OVERWRITE) - || (pub->chunk && !(frame % pub->chunk))) { - /* Clear the slate. */ - img->currentframe = -1; - memset(img->framedata[0], 0, pub->width * pub->height); - memset(img->mask[0], 0, pub->width * pub->height); - } else { - /* Start over if we are backtracking. */ - if (img->currentframe > frame) { - memset(img->mask[0], 0, pub->width * pub->height); - img->currentframe = -1; - } - - /* We must have previous frame decoded to continue. */ - if (frame > img->currentframe + 1) { - if (!lbx_img_getframe(pub, frame-1)) - return NULL; + 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; } } - if (img->currentframe != frame) { - int rc; - - if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) { - return NULL; - } - - /* Read frame header */ - if (img->fops->read(buf, 4, img->f) != 4) { - if (img->fops->eof(img->f)) - lbx_error_raise(LBX_EEOF); - return NULL; - } - - if (unpack_16_le(buf) != 1) { - lbx_error_raise(LBX_EFORMAT); - return NULL; - } - - img->currentx = 0; - img->currenty = unpack_16_le(buf+2); - if (img->currenty > img->pub.height) { - lbx_error_raise(LBX_EFORMAT); - return NULL; - } - - do { - rc = _lbx_drawrow(img); - if (rc == -1) - return NULL; + img->read_state = READ_STATE_HEADER; + img->currentx += img->currentn; - if (img->fops->tell(img->f) > img->offsets[frame+1]) { - lbx_error_raise(LBX_EFORMAT); - return NULL; - } - } while (!rc); - } - - img->currentframe = frame; - return img->framedata; + return img->currentn; } int @@ -502,35 +435,15 @@ void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info) } } -unsigned char **lbx_img_getmask(struct lbx_image *pub) -{ - struct lbx_image_priv *img = (struct lbx_image_priv *)pub; - - return img->mask; -} - int lbx_img_close(struct lbx_image *pub) { struct lbx_image_priv *img = (struct lbx_image_priv *)pub; int rc = 0; - if (img) { - if (img->framedata) { - free(img->framedata[0]); - free(img->framedata); - } - - if (img->mask) { - free(img->mask[0]); - free(img->mask); - } - - if (img && img->dtor) { - rc = img->dtor(img->f); - } - - free(img); + if (img && img->dtor) { + rc = img->dtor(img->f); } + free(img); return rc; }