2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright © 2006-2011, 2013-2014 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/>.
32 #define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */
33 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
34 #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */
35 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
36 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
38 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
42 /* States for image readout */
50 struct lbx_image_priv {
53 unsigned short wtf, flags;
55 unsigned short palstart, palcount;
57 const struct lbx_file_ops *fops;
58 int (*dtor)(void *handle);
63 /* State of frame readout */
64 unsigned currentx, currenty, currentn;
68 unsigned char **framedata;
71 unsigned long offsets[];
74 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
76 unsigned short nframes = unpack_16_le(hdr+6);
77 struct lbx_image_priv *img;
79 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
81 lbx_error_raise(LBX_ENOMEM);
85 *img = (struct lbx_image_priv) {
86 .pub.width = unpack_16_le(hdr+0),
87 .pub.height = unpack_16_le(hdr+2),
88 .wtf = unpack_16_le(hdr+4),
93 .flags = unpack_16_le(hdr+10),
98 if (img->flags & FLAG_OVERWRITE)
101 if (img->flags & FLAG_LOOPING)
107 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
108 int (*destructor)(void *))
110 unsigned char hdr_buf[HDR_LEN];
111 struct lbx_image_priv *img;
113 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
115 lbx_error_raise(LBX_EEOF);
119 img = lbx_img_init(hdr_buf);
125 img->dtor = destructor;
128 * DEBUG ONLY. These assertions exist to catch otherwise valid image
129 * files which differ from what I believe to be true of all LBX images.
130 * When we can decode every image, then these assertions should be
131 * replaced with constraints.
133 _lbx_assert(img->wtf == 0); /* version? */
134 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
135 _lbx_assert(img->pub.frames > img->pub.leadin);
136 _lbx_assert(!(img->flags & ~FLAG_ALL));
138 /* Read all offsets. Should be merged with identical code in lbx.c */
139 for (unsigned i = 0; i <= img->pub.frames; i++) {
140 unsigned char buf[4];
142 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
144 lbx_error_raise(LBX_EEOF);
149 img->offsets[i] = unpack_32_le(buf);
152 if (img->flags & FLAG_PALETTE) {
153 unsigned char buf[4];
155 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
157 lbx_error_raise(LBX_EEOF);
162 img->palstart = unpack_16_le(buf+0);
163 img->palcount = unpack_16_le(buf+2);
164 img->paloff = fops->tell(f);
166 if (img->palstart + img->palcount > 256) {
167 lbx_error_raise(LBX_EFORMAT);
176 static int pipe_close(void *f)
178 struct lbx_pipe_state *p = f;
186 static int file_close(void *f)
188 return fclose((FILE *)f);
191 struct lbx_image *lbx_img_fopen(const char *file)
193 struct lbx_pipe_state *p;
196 f = fopen(file, "rb");
198 lbx_error_raise(-errno);
202 if (fseek(f, 0, SEEK_CUR) == 0)
203 return lbx_img_open(f, &lbx_default_fops, file_close);
205 p = malloc(sizeof *p);
207 lbx_error_raise(LBX_ENOMEM);
212 *p = (struct lbx_pipe_state) { .f = f };
213 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
216 static int _lbx_drawrow(struct lbx_image_priv *img)
222 assert(img->framedata);
225 rc = lbx_img_read_row_header(&img->pub, &x, &y);
231 pos = &img->framedata[y][x];
232 rc = lbx_img_read_row_data(&img->pub, pos);
235 memset(&img->mask[y][x], 1, rc);
240 static unsigned char **allocframebuffer(size_t width, size_t height)
242 unsigned char **new, *tmp;
245 if (height > SIZE_MAX / sizeof *new) {
246 lbx_error_raise(LBX_ENOMEM);
250 /* Ensure that there is at least one row in the framebuffer. */
251 if (height == 0 || width == 0)
254 tmp = calloc(height, width);
256 lbx_error_raise(LBX_ENOMEM);
260 new = malloc(height * sizeof *new);
262 lbx_error_raise(LBX_ENOMEM);
267 for (i = 0; i < height; i++) {
268 new[i] = tmp + i * width;
274 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
276 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
278 if (frame >= pub->frames) {
279 lbx_error_raise(LBX_EINVAL);
283 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
287 if (!(img->flags & FLAG_RAW)) {
288 unsigned char buf[4];
290 /* Read frame header */
291 if (img->fops->read(buf, 4, img->f) != 4) {
292 if (img->fops->eof(img->f))
293 lbx_error_raise(LBX_EEOF);
297 if (unpack_16_le(buf) != 1) {
298 lbx_error_raise(LBX_EFORMAT);
303 img->currenty = unpack_16_le(buf+2);
304 if (img->currenty > img->pub.height) {
305 lbx_error_raise(LBX_EFORMAT);
310 img->read_state = READ_STATE_HEADER;
314 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
316 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
317 unsigned short length, offset;
318 unsigned char buf[4];
320 if (img->read_state != READ_STATE_HEADER) {
321 lbx_error_raise(LBX_EINVAL);
325 /* Raw images have no row headers */
326 if (img->flags & FLAG_RAW) {
327 img->currentn = img->pub.width;
328 *y = img->currenty++;
331 if (*y < img->pub.height) {
332 img->read_state = READ_STATE_DATA;
333 return img->currentn;
335 img->read_state = READ_STATE_DONE;
341 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
342 if (img->fops->eof(img->f))
343 lbx_error_raise(LBX_EEOF);
347 length = unpack_16_le(buf+0);
348 offset = unpack_16_le(buf+2);
351 if (offset == 1000) {
352 img->read_state = READ_STATE_DONE;
354 } else if (offset > img->pub.height - img->currenty) {
355 lbx_error_raise(LBX_EFORMAT);
359 img->currenty += offset;
362 } while (length == 0);
364 if (offset > img->pub.width - img->currentx) {
365 lbx_error_raise(LBX_EFORMAT);
368 img->currentx += offset;
370 if (length > img->pub.width - img->currentx) {
371 lbx_error_raise(LBX_EFORMAT);
374 img->currentn = length;
376 img->read_state = READ_STATE_DATA;
380 return img->currentn;
383 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
385 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
387 if (img->read_state != READ_STATE_DATA) {
388 lbx_error_raise(LBX_EINVAL);
392 if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
393 if (img->fops->eof(img->f))
394 lbx_error_raise(LBX_EEOF);
398 if (!(img->flags & FLAG_RAW)) {
399 /* Skip padding byte, if any */
400 if (img->currentn % 2) {
401 if (img->fops->seek(img->f, 1, SEEK_CUR))
406 img->read_state = READ_STATE_HEADER;
407 img->currentx += img->currentn;
409 return img->currentn;
412 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
414 unsigned long size = img->pub.width * img->pub.height;
417 assert(img->flags & FLAG_RAW);
419 rc = lbx_img_seek(&img->pub, frame);
423 if (img->fops->read(img->framedata[0], size, img->f) != size) {
424 if (img->fops->eof(img->f))
425 lbx_error_raise(LBX_EEOF);
428 memset(img->mask[0], 1, size);
430 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
431 lbx_error_raise(LBX_EFORMAT);
435 return img->framedata;
438 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
440 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
441 unsigned char buf[4];
443 if (frame >= pub->frames || frame < 0) {
444 lbx_error_raise(LBX_EINVAL);
448 if (!img->framedata) {
449 img->framedata = allocframebuffer(pub->width, pub->height);
455 img->mask = allocframebuffer(pub->width, pub->height);
460 if (img->flags & FLAG_RAW)
461 return read_raw_frame(img, frame);
463 if ((img->flags & FLAG_OVERWRITE)
464 || (pub->chunk && !(frame % pub->chunk))) {
465 /* Clear the slate. */
466 img->currentframe = -1;
467 memset(img->framedata[0], 0, pub->width * pub->height);
468 memset(img->mask[0], 0, pub->width * pub->height);
470 /* Start over if we are backtracking. */
471 if (img->currentframe > frame) {
472 memset(img->mask[0], 0, pub->width * pub->height);
473 img->currentframe = -1;
476 /* We must have previous frame decoded to continue. */
477 if (frame > img->currentframe + 1) {
478 if (!lbx_img_getframe(pub, frame-1))
483 if (img->currentframe != frame) {
486 rc = lbx_img_seek(pub, frame);
491 rc = _lbx_drawrow(img);
495 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
496 lbx_error_raise(LBX_EFORMAT);
502 img->currentframe = frame;
503 return img->framedata;
507 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
508 struct lbx_colour palette[static 256])
510 unsigned char entry[4];
513 for (i = 0; i < 256; i++) {
514 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
516 lbx_error_raise(LBX_EEOF);
521 lbx_error_raise(LBX_EFORMAT);
525 palette[i] = (struct lbx_colour) {
526 .red = entry[1] & 0x3f,
527 .green = entry[2] & 0x3f,
528 .blue = entry[3] & 0x3f,
537 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
539 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
540 unsigned char entry[4];
544 /* Do nothing if the image doesn't have embedded palette data. */
545 if (!(img->flags & FLAG_PALETTE))
548 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
552 for (i = 0; i < img->palcount; i++) {
553 rc = img->fops->read(entry, sizeof entry, img->f);
554 if (rc < sizeof entry) {
559 lbx_error_raise(LBX_EFORMAT);
563 palette[img->palstart + i] = (struct lbx_colour){
573 if (img->fops->eof(img->f))
574 lbx_error_raise(LBX_EEOF);
578 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
580 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
582 *info = (struct lbx_imginfo) {
583 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
586 /* There seems to be two ways of specifying that an image loops. */
587 if (img->flags & FLAG_LOOPING) {
590 } else if (img->pub.leadin != pub->frames - 1) {
591 info->loopstart = img->pub.leadin;
596 unsigned char **lbx_img_getmask(struct lbx_image *pub)
598 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
603 int lbx_img_close(struct lbx_image *pub)
605 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
609 if (img->framedata) {
610 free(img->framedata[0]);
611 free(img->framedata);
619 if (img && img->dtor) {
620 rc = img->dtor(img->f);