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 struct lbx_image_priv {
45 unsigned short wtf, flags;
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_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
65 unsigned short nframes = unpack_16_le(hdr+6);
66 struct lbx_image_priv *img;
68 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
70 lbx_error_raise(LBX_ENOMEM);
74 *img = (struct lbx_image_priv) {
75 .pub.width = unpack_16_le(hdr+0),
76 .pub.height = unpack_16_le(hdr+2),
77 .wtf = unpack_16_le(hdr+4),
82 .flags = unpack_16_le(hdr+10),
87 if (img->flags & FLAG_OVERWRITE)
90 if (img->flags & FLAG_LOOPING)
96 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
97 int (*destructor)(void *))
99 unsigned char hdr_buf[HDR_LEN];
100 struct lbx_image_priv *img;
102 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
104 lbx_error_raise(LBX_EEOF);
108 img = lbx_img_init(hdr_buf);
114 img->dtor = destructor;
117 * DEBUG ONLY. These assertions exist to catch otherwise valid image
118 * files which differ from what I believe to be true of all LBX images.
119 * When we can decode every image, then these assertions should be
120 * replaced with constraints.
122 _lbx_assert(img->wtf == 0); /* version? */
123 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
124 _lbx_assert(img->pub.frames > img->pub.leadin);
125 _lbx_assert(!(img->flags & ~FLAG_ALL));
127 /* Read all offsets. Should be merged with identical code in lbx.c */
128 for (unsigned i = 0; i <= img->pub.frames; i++) {
129 unsigned char buf[4];
131 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
133 lbx_error_raise(LBX_EEOF);
138 img->offsets[i] = unpack_32_le(buf);
141 if (img->flags & FLAG_PALETTE) {
142 unsigned char buf[4];
144 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
146 lbx_error_raise(LBX_EEOF);
151 img->palstart = unpack_16_le(buf+0);
152 img->palcount = unpack_16_le(buf+2);
153 img->paloff = fops->tell(f);
155 if (img->palstart + img->palcount > 256) {
156 lbx_error_raise(LBX_EFORMAT);
165 static int pipe_close(void *f)
167 struct lbx_pipe_state *p = f;
175 static int file_close(void *f)
177 return fclose((FILE *)f);
180 struct lbx_image *lbx_img_fopen(const char *file)
182 struct lbx_pipe_state *p;
185 f = fopen(file, "rb");
187 lbx_error_raise(-errno);
191 if (fseek(f, 0, SEEK_CUR) == 0)
192 return lbx_img_open(f, &lbx_default_fops, file_close);
194 p = malloc(sizeof *p);
196 lbx_error_raise(LBX_ENOMEM);
201 *p = (struct lbx_pipe_state) { .f = f };
202 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
205 static int _lbx_drawrow(struct lbx_image_priv *img)
207 unsigned short length, offset;
208 unsigned char buf[4];
212 assert(img->framedata);
215 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
218 length = unpack_16_le(buf+0);
219 offset = unpack_16_le(buf+2);
220 if (length == 0 && offset == 1000)
223 /* Length of 0 increments Y position */
225 if (offset > img->pub.height - img->currenty) {
226 lbx_error_raise(LBX_EFORMAT);
230 img->currenty += offset;
235 /* Otherwise we read pixel data */
236 if (offset > img->pub.width - img->currentx) {
237 lbx_error_raise(LBX_EFORMAT);
240 img->currentx += offset;
242 if (length > img->pub.width - img->currentx) {
243 lbx_error_raise(LBX_EFORMAT);
247 memset(&img->mask[img->currenty][img->currentx], 1, length);
249 pos = &img->framedata[img->currenty][img->currentx];
250 rc = img->fops->read(pos, length, img->f);
256 /* Skip padding byte, if any */
258 if (img->fops->read(buf, 1, img->f) != 1)
264 if (img->fops->eof(img->f))
265 lbx_error_raise(LBX_EEOF);
269 static unsigned char **allocframebuffer(size_t width, size_t height)
271 unsigned char **new, *tmp;
274 if (height > SIZE_MAX / sizeof *new) {
275 lbx_error_raise(LBX_ENOMEM);
279 /* Ensure that there is at least one row in the framebuffer. */
280 if (height == 0 || width == 0)
283 tmp = calloc(height, width);
285 lbx_error_raise(LBX_ENOMEM);
289 new = malloc(height * sizeof *new);
291 lbx_error_raise(LBX_ENOMEM);
296 for (i = 0; i < height; i++) {
297 new[i] = tmp + i * width;
303 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
305 unsigned long size = img->pub.width * img->pub.height;
307 assert(img->flags & FLAG_RAW);
309 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
313 if (img->fops->read(img->framedata[0], size, img->f) != size) {
314 if (img->fops->eof(img->f))
315 lbx_error_raise(LBX_EEOF);
318 memset(img->mask[0], 1, size);
320 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
321 lbx_error_raise(LBX_EFORMAT);
325 return img->framedata;
328 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
330 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
331 unsigned char buf[4];
333 if (frame >= pub->frames || frame < 0) {
334 lbx_error_raise(LBX_ENOENT);
338 if (!img->framedata) {
339 img->framedata = allocframebuffer(pub->width, pub->height);
345 img->mask = allocframebuffer(pub->width, pub->height);
350 if (img->flags & FLAG_RAW)
351 return read_raw_frame(img, frame);
353 if ((img->flags & FLAG_OVERWRITE)
354 || (pub->chunk && !(frame % pub->chunk))) {
355 /* Clear the slate. */
356 img->currentframe = -1;
357 memset(img->framedata[0], 0, pub->width * pub->height);
358 memset(img->mask[0], 0, pub->width * pub->height);
360 /* Start over if we are backtracking. */
361 if (img->currentframe > frame) {
362 memset(img->mask[0], 0, pub->width * pub->height);
363 img->currentframe = -1;
366 /* We must have previous frame decoded to continue. */
367 if (frame > img->currentframe + 1) {
368 if (!lbx_img_getframe(pub, frame-1))
373 if (img->currentframe != frame) {
376 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
380 /* Read frame header */
381 if (img->fops->read(buf, 4, img->f) != 4) {
382 if (img->fops->eof(img->f))
383 lbx_error_raise(LBX_EEOF);
387 if (unpack_16_le(buf) != 1) {
388 lbx_error_raise(LBX_EFORMAT);
393 img->currenty = unpack_16_le(buf+2);
394 if (img->currenty > img->pub.height) {
395 lbx_error_raise(LBX_EFORMAT);
400 rc = _lbx_drawrow(img);
404 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
405 lbx_error_raise(LBX_EFORMAT);
411 img->currentframe = frame;
412 return img->framedata;
416 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
417 struct lbx_colour palette[static 256])
419 unsigned char entry[4];
422 for (i = 0; i < 256; i++) {
423 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
425 lbx_error_raise(LBX_EEOF);
430 lbx_error_raise(LBX_EFORMAT);
434 palette[i] = (struct lbx_colour) {
435 .red = entry[1] & 0x3f,
436 .green = entry[2] & 0x3f,
437 .blue = entry[3] & 0x3f,
446 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
448 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
449 unsigned char entry[4];
453 /* Do nothing if the image doesn't have embedded palette data. */
454 if (!(img->flags & FLAG_PALETTE))
457 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
461 for (i = 0; i < img->palcount; i++) {
462 rc = img->fops->read(entry, sizeof entry, img->f);
463 if (rc < sizeof entry) {
468 lbx_error_raise(LBX_EFORMAT);
472 palette[img->palstart + i] = (struct lbx_colour){
482 if (img->fops->eof(img->f))
483 lbx_error_raise(LBX_EEOF);
487 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
489 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
491 *info = (struct lbx_imginfo) {
492 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
495 /* There seems to be two ways of specifying that an image loops. */
496 if (img->flags & FLAG_LOOPING) {
499 } else if (img->pub.leadin != pub->frames - 1) {
500 info->loopstart = img->pub.leadin;
505 unsigned char **lbx_img_getmask(struct lbx_image *pub)
507 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
512 int lbx_img_close(struct lbx_image *pub)
514 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
518 if (img->framedata) {
519 free(img->framedata[0]);
520 free(img->framedata);
528 if (img && img->dtor) {
529 rc = img->dtor(img->f);