2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright (C) 2006-2008 Nick Bowler
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
33 #define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */
34 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
35 #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */
36 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
37 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
39 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
44 unsigned short width, height;
45 unsigned short wtf, flags;
46 unsigned char frames, wtf2, leadin, chunk;
47 unsigned short palstart, palcount;
49 const struct lbx_file_ops *fops;
50 int (*dtor)(void *handle);
56 int currentx, currenty;
57 unsigned char **framedata;
60 unsigned long offsets[];
63 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
65 unsigned short nframes = unpack_16_le(hdr+6);
66 struct lbx_image *img;
68 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
74 *img = (struct lbx_image) {
75 .width = unpack_16_le(hdr+0),
76 .height = unpack_16_le(hdr+2),
77 .wtf = unpack_16_le(hdr+4),
82 .flags = unpack_16_le(hdr+10),
90 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
91 int (*destructor)(void *))
93 unsigned char hdr_buf[HDR_LEN];
94 struct lbx_image *img;
96 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
103 img = lbximg_init(hdr_buf);
109 img->dtor = destructor;
112 * DEBUG ONLY. These assertions exist to catch otherwise valid image
113 * files which differ from what I believe to be true of all LBX images.
114 * When we can decode every image, then these assertions should be
115 * replaced with constraints.
117 _lbx_assert(img->wtf == 0); /* version? */
118 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
119 _lbx_assert(img->frames > img->leadin);
120 _lbx_assert(!(img->flags & ~FLAG_ALL));
122 /* Read all offsets. Should be merged with identical code in lbx.c */
123 for (unsigned i = 0; i <= img->frames; i++) {
124 unsigned char buf[4];
126 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
129 lbx_errno = LBX_EEOF;
134 img->offsets[i] = unpack_32_le(buf);
137 if (img->flags & FLAG_PALETTE) {
138 unsigned char buf[4];
140 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
143 lbx_errno = LBX_EEOF;
148 img->palstart = unpack_16_le(buf+0);
149 img->palcount = unpack_16_le(buf+2);
150 img->paloff = fops->tell(f);
152 if (img->palstart + img->palcount > 256) {
153 lbx_errno = LBX_EFORMAT;
162 struct lbx_image *lbximg_fopen(FILE *f)
164 return lbximg_open(f, &lbx_default_fops, NULL);
167 static int _lbx_drawrow(int first, struct lbx_image *img)
169 unsigned short type, count, yval, xval;
170 unsigned char buf[4];
174 assert(img->framedata);
177 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
179 type = unpack_16_le(buf+0);
188 yval = unpack_16_le(buf+2);
192 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
194 count = unpack_16_le(buf+0);
196 xval = unpack_16_le(buf+2);
200 /* Ensure that the row fits in the image. */
201 if (img->height - img->currenty <= yval || xval >= img->width) {
202 lbx_errno = LBX_EFORMAT;
206 img->currenty += yval;
207 img->currentx = xval;
209 xval = unpack_16_le(buf+2);
211 if (img->width - img->currentx <= xval) {
212 lbx_errno = LBX_EFORMAT;
215 img->currentx += xval;
220 if (count > img->width - img->currentx) {
221 lbx_errno = LBX_EFORMAT;
225 memset(&img->mask[img->currenty][img->currentx], 1, count);
227 pos = &img->framedata[img->currenty][img->currentx];
228 rc = img->fops->read(pos, count, img->f);
235 if (img->fops->read(buf, 1, img->f) != 1)
242 if (img->fops->eof(img->f))
243 lbx_errno = LBX_EEOF;
247 static unsigned char **allocframebuffer(size_t width, size_t height)
249 unsigned char **new, *tmp;
252 tmp = calloc(height, width);
258 new = malloc(height * sizeof *new);
265 for (i = 0; i < height; i++) {
266 new[i] = tmp + i * width;
272 static unsigned char **read_raw_frame(struct lbx_image *img, int frame)
274 unsigned long size = img->width * img->height;
276 assert(img->flags & FLAG_RAW);
278 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
283 if (img->fops->read(img->framedata[0], size, img->f) != size) {
285 if (img->fops->eof(img->f))
286 lbx_errno = LBX_EEOF;
289 memset(img->mask[0], 1, size);
291 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
292 lbx_errno = LBX_EFORMAT;
296 return img->framedata;
299 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
301 if (frame >= img->frames || frame < 0) {
302 lbx_errno = LBX_ERANGE;
306 if (!img->framedata) {
307 img->framedata = allocframebuffer(img->width, img->height);
313 img->mask = allocframebuffer(img->width, img->height);
318 if (img->flags & FLAG_RAW)
319 return read_raw_frame(img, frame);
321 if ((img->flags & FLAG_OVERWRITE)
322 || (img->chunk && !(frame % img->chunk))) {
323 /* Clear the slate. */
324 img->currentframe = -1;
325 memset(img->framedata[0], 0, img->width * img->height);
326 memset(img->mask[0], 0, img->width * img->height);
328 /* Start over if we are backtracking. */
329 if (img->currentframe > frame) {
330 memset(img->mask[0], 0, img->width * img->height);
331 img->currentframe = -1;
334 /* We must have previous frame decoded to continue. */
335 if (frame > img->currentframe + 1) {
336 if (!lbximg_getframe(img, frame-1))
341 if (img->currentframe != frame) {
344 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
350 rc = _lbx_drawrow(first, img);
355 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
356 lbx_errno = LBX_EFORMAT;
362 img->currentframe = frame;
363 return img->framedata;
367 lbximg_loadpalette(void *f, const struct lbx_file_ops *fops,
368 struct lbx_colour palette[static 256])
370 unsigned char entry[4];
373 for (i = 0; i < 256; i++) {
374 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
375 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
380 lbx_errno = LBX_EFORMAT;
384 palette[i] = (struct lbx_colour) {
385 .red = entry[1] << 2,
386 .green = entry[2] << 2,
387 .blue = entry[3] << 2,
396 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
398 unsigned char entry[4];
402 /* Do nothing if the image doesn't have embedded palette data. */
403 if (!(img->flags & FLAG_PALETTE))
406 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
411 for (i = 0; i < img->palcount; i++) {
412 rc = img->fops->read(entry, sizeof entry, img->f);
413 if (rc < sizeof entry) {
418 lbx_errno = LBX_EFORMAT;
422 palette[img->palstart + i] = (struct lbx_colour){
423 .red = entry[1] << 2,
424 .green = entry[2] << 2,
425 .blue = entry[3] << 2,
432 lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
436 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
438 *info = (struct lbx_imginfo) {
440 .height = img->height,
441 .nframes = img->frames,
443 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
446 /* There seems to be two ways of specifying that an image loops. */
447 if (img->flags & FLAG_LOOPING) {
450 } else if (img->leadin != img->frames - 1) {
451 info->loopstart = img->leadin;
456 unsigned char **lbximg_getmask(struct lbx_image *img)
461 int lbximg_close(struct lbx_image *img)
466 if (img->framedata) {
467 free(img->framedata[0]);
468 free(img->framedata);
476 if (img && img->dtor) {
477 rc = img->dtor(img->f);