From: Nick Bowler Date: Tue, 9 Feb 2010 04:14:16 +0000 (-0500) Subject: liblbx: Add support for "raw" LBX images. X-Git-Url: http://git.draconx.ca/gitweb/liblbx.git/commitdiff_plain/bdad20799885211ac4c76bdaef5b027552d6241d liblbx: Add support for "raw" LBX images. There is a new image format in town. If a particular flag bit is set, then there are no row headers and data for every pixel of a frame is simply stored in row-major order. An example of such an image is starbg.lbx.009, as well as several others in the same archive. Don't you just *love* the designers of this format? --- diff --git a/src/image.c b/src/image.c index 76fbfed..21f5a8e 100644 --- a/src/image.c +++ b/src/image.c @@ -30,12 +30,13 @@ #include "lbx.h" #include "image.h" +#define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */ #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */ #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */ #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */ #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */ -#define FLAG_ALL (FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING) +#define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING) #define HDR_LEN 12 @@ -265,6 +266,33 @@ static unsigned char **allocframebuffer(size_t width, size_t height) return new; } +static unsigned char **read_raw_frame(struct lbx_image *img, int frame) +{ + unsigned long size = img->width * img->height; + + assert(img->flags & FLAG_RAW); + + if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) { + lbx_errno = -errno; + return NULL; + } + + if (img->fops->read(img->framedata[0], size, img->f) != size) { + lbx_errno = -errno; + if (img->fops->eof(img->f)) + lbx_errno = LBX_EEOF; + return NULL; + } + memset(img->mask[0], 1, size); + + if (img->fops->tell(img->f) > img->offsets[frame+1]) { + lbx_errno = LBX_EFORMAT; + return NULL; + } + + return img->framedata; +} + unsigned char **lbximg_getframe(struct lbx_image *img, int frame) { if (frame >= img->frames || frame < 0) { @@ -284,6 +312,9 @@ unsigned char **lbximg_getframe(struct lbx_image *img, int frame) return NULL; } + if (img->flags & FLAG_RAW) + return read_raw_frame(img, frame); + if (img->flags & FLAG_OVERWRITE) { /* Clear the slate. */ img->currentframe = -1; diff --git a/tests/regress.zsh b/tests/regress.zsh index 20239b5..0131fbd 100755 --- a/tests/regress.zsh +++ b/tests/regress.zsh @@ -75,6 +75,13 @@ echo "ships.lbx.042: single frame, external+override palette, transparency:" $LBXIMG -df ships.lbx.042 -p fonts.lbx.012 -O ships.lbx.049 compare 0 bd643736d46ef387bcffcc8803aabb83 +# Nebulae +$LBXTOOL -xf $DATADIR/starbg.lbx starbg.lbx.009 + +echo "starbg.lbx.009: single frame, raw data:" +$LBXIMG -df starbg.lbx.009 -p fonts.lbx.005 +compare 0 cfc5d92b6503951c4962498c7dcfea31 + # Clean up if [[ $FAILED -eq 0 ]]; then echo "All tests completed successfully."