2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright © 2006-2011 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/>.
31 #define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */
32 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
33 #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */
34 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
35 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
37 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
41 struct lbx_image_priv {
44 unsigned short wtf, flags;
45 unsigned char wtf2, leadin;
46 unsigned short palstart, palcount;
48 const struct lbx_file_ops *fops;
49 int (*dtor)(void *handle);
55 int currentx, currenty;
56 unsigned char **framedata;
59 unsigned long offsets[];
62 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
64 unsigned short nframes = unpack_16_le(hdr+6);
65 struct lbx_image_priv *img;
67 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
69 lbx_error_raise(LBX_ENOMEM);
73 *img = (struct lbx_image_priv) {
74 .pub.width = unpack_16_le(hdr+0),
75 .pub.height = unpack_16_le(hdr+2),
76 .wtf = unpack_16_le(hdr+4),
81 .flags = unpack_16_le(hdr+10),
89 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
90 int (*destructor)(void *))
92 unsigned char hdr_buf[HDR_LEN];
93 struct lbx_image_priv *img;
95 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
97 lbx_error_raise(LBX_EEOF);
101 img = lbx_img_init(hdr_buf);
107 img->dtor = destructor;
110 * DEBUG ONLY. These assertions exist to catch otherwise valid image
111 * files which differ from what I believe to be true of all LBX images.
112 * When we can decode every image, then these assertions should be
113 * replaced with constraints.
115 _lbx_assert(img->wtf == 0); /* version? */
116 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
117 _lbx_assert(img->pub.frames > img->leadin);
118 _lbx_assert(!(img->flags & ~FLAG_ALL));
120 /* Read all offsets. Should be merged with identical code in lbx.c */
121 for (unsigned i = 0; i <= img->pub.frames; i++) {
122 unsigned char buf[4];
124 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
126 lbx_error_raise(LBX_EEOF);
131 img->offsets[i] = unpack_32_le(buf);
134 if (img->flags & FLAG_PALETTE) {
135 unsigned char buf[4];
137 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
139 lbx_error_raise(LBX_EEOF);
144 img->palstart = unpack_16_le(buf+0);
145 img->palcount = unpack_16_le(buf+2);
146 img->paloff = fops->tell(f);
148 if (img->palstart + img->palcount > 256) {
149 lbx_error_raise(LBX_EFORMAT);
158 static int pipe_close(void *f)
160 struct lbx_pipe_state *p = f;
168 static int file_close(void *f)
170 return fclose((FILE *)f);
173 struct lbx_image *lbx_img_fopen(const char *file)
175 struct lbx_pipe_state *p;
178 f = fopen(file, "rb");
182 if (fseek(f, 0, SEEK_CUR) == 0)
183 return lbx_img_open(f, &lbx_default_fops, file_close);
185 p = malloc(sizeof *p);
191 *p = (struct lbx_pipe_state) { .f = f };
192 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
195 static int _lbx_drawrow(int first, struct lbx_image_priv *img)
197 unsigned short type, count, yval, xval;
198 unsigned char buf[4];
202 assert(img->framedata);
205 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
207 type = unpack_16_le(buf+0);
216 yval = unpack_16_le(buf+2);
220 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
222 count = unpack_16_le(buf+0);
224 xval = unpack_16_le(buf+2);
228 /* Ensure that the row fits in the image. */
229 if (img->pub.height - img->currenty <= yval
230 || xval >= img->pub.width) {
231 lbx_error_raise(LBX_EFORMAT);
235 img->currenty += yval;
236 img->currentx = xval;
238 xval = unpack_16_le(buf+2);
240 if (img->pub.width - img->currentx <= xval) {
241 lbx_error_raise(LBX_EFORMAT);
244 img->currentx += xval;
249 if (count > img->pub.width - img->currentx) {
250 lbx_error_raise(LBX_EFORMAT);
254 memset(&img->mask[img->currenty][img->currentx], 1, count);
256 pos = &img->framedata[img->currenty][img->currentx];
257 rc = img->fops->read(pos, count, img->f);
264 if (img->fops->read(buf, 1, img->f) != 1)
270 if (img->fops->eof(img->f))
271 lbx_error_raise(LBX_EEOF);
275 static unsigned char **allocframebuffer(size_t width, size_t height)
277 unsigned char **new, *tmp;
280 tmp = calloc(height, width);
282 lbx_error_raise(LBX_ENOMEM);
286 new = malloc(height * sizeof *new);
288 lbx_error_raise(LBX_ENOMEM);
293 for (i = 0; i < height; i++) {
294 new[i] = tmp + i * width;
300 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
302 unsigned long size = img->pub.width * img->pub.height;
304 assert(img->flags & FLAG_RAW);
306 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
310 if (img->fops->read(img->framedata[0], size, img->f) != size) {
311 if (img->fops->eof(img->f))
312 lbx_error_raise(LBX_EEOF);
315 memset(img->mask[0], 1, size);
317 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
318 lbx_error_raise(LBX_EFORMAT);
322 return img->framedata;
325 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
327 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
329 if (frame >= pub->frames || frame < 0) {
330 lbx_error_raise(LBX_ENOENT);
334 if (!img->framedata) {
335 img->framedata = allocframebuffer(pub->width, pub->height);
341 img->mask = allocframebuffer(pub->width, pub->height);
346 if (img->flags & FLAG_RAW)
347 return read_raw_frame(img, frame);
349 if ((img->flags & FLAG_OVERWRITE)
350 || (pub->chunk && !(frame % pub->chunk))) {
351 /* Clear the slate. */
352 img->currentframe = -1;
353 memset(img->framedata[0], 0, pub->width * pub->height);
354 memset(img->mask[0], 0, pub->width * pub->height);
356 /* Start over if we are backtracking. */
357 if (img->currentframe > frame) {
358 memset(img->mask[0], 0, pub->width * pub->height);
359 img->currentframe = -1;
362 /* We must have previous frame decoded to continue. */
363 if (frame > img->currentframe + 1) {
364 if (!lbx_img_getframe(pub, frame-1))
369 if (img->currentframe != frame) {
372 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
377 rc = _lbx_drawrow(first, img);
382 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
383 lbx_error_raise(LBX_EFORMAT);
389 img->currentframe = frame;
390 return img->framedata;
394 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
395 struct lbx_colour palette[static 256])
397 unsigned char entry[4];
400 for (i = 0; i < 256; i++) {
401 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
403 lbx_error_raise(LBX_EEOF);
408 lbx_error_raise(LBX_EFORMAT);
412 palette[i] = (struct lbx_colour) {
413 .red = entry[1] << 2,
414 .green = entry[2] << 2,
415 .blue = entry[3] << 2,
424 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
426 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
427 unsigned char entry[4];
431 /* Do nothing if the image doesn't have embedded palette data. */
432 if (!(img->flags & FLAG_PALETTE))
435 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
439 for (i = 0; i < img->palcount; i++) {
440 rc = img->fops->read(entry, sizeof entry, img->f);
441 if (rc < sizeof entry) {
446 lbx_error_raise(LBX_EFORMAT);
450 palette[img->palstart + i] = (struct lbx_colour){
451 .red = entry[1] << 2,
452 .green = entry[2] << 2,
453 .blue = entry[3] << 2,
460 if (img->fops->eof(img->f))
461 lbx_error_raise(LBX_EEOF);
465 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
467 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
469 *info = (struct lbx_imginfo) {
471 .height = pub->height,
472 .nframes = pub->frames,
474 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
477 /* There seems to be two ways of specifying that an image loops. */
478 if (img->flags & FLAG_LOOPING) {
481 } else if (img->leadin != pub->frames - 1) {
482 info->loopstart = img->leadin;
487 unsigned char **lbx_img_getmask(struct lbx_image *pub)
489 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
494 int lbx_img_close(struct lbx_image *pub)
496 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
500 if (img->framedata) {
501 free(img->framedata[0]);
502 free(img->framedata);
510 if (img && img->dtor) {
511 rc = img->dtor(img->f);