2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright © 2006-2011, 2013 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");
180 lbx_error_raise(-errno);
184 if (fseek(f, 0, SEEK_CUR) == 0)
185 return lbx_img_open(f, &lbx_default_fops, file_close);
187 p = malloc(sizeof *p);
189 lbx_error_raise(LBX_ENOMEM);
194 *p = (struct lbx_pipe_state) { .f = f };
195 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
198 static int _lbx_drawrow(int first, struct lbx_image_priv *img)
200 unsigned short type, count, yval, xval;
201 unsigned char buf[4];
205 assert(img->framedata);
208 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
210 type = unpack_16_le(buf+0);
219 yval = unpack_16_le(buf+2);
223 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
225 count = unpack_16_le(buf+0);
227 xval = unpack_16_le(buf+2);
231 /* Ensure that the row fits in the image. */
232 if (img->pub.height - img->currenty <= yval
233 || xval >= img->pub.width) {
234 lbx_error_raise(LBX_EFORMAT);
238 img->currenty += yval;
239 img->currentx = xval;
241 xval = unpack_16_le(buf+2);
243 if (img->pub.width - img->currentx <= xval) {
244 lbx_error_raise(LBX_EFORMAT);
247 img->currentx += xval;
252 if (count > img->pub.width - img->currentx) {
253 lbx_error_raise(LBX_EFORMAT);
257 memset(&img->mask[img->currenty][img->currentx], 1, count);
259 pos = &img->framedata[img->currenty][img->currentx];
260 rc = img->fops->read(pos, count, img->f);
267 if (img->fops->read(buf, 1, img->f) != 1)
273 if (img->fops->eof(img->f))
274 lbx_error_raise(LBX_EEOF);
278 static unsigned char **allocframebuffer(size_t width, size_t height)
280 unsigned char **new, *tmp;
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;
332 if (frame >= pub->frames || frame < 0) {
333 lbx_error_raise(LBX_ENOENT);
337 if (!img->framedata) {
338 img->framedata = allocframebuffer(pub->width, pub->height);
344 img->mask = allocframebuffer(pub->width, pub->height);
349 if (img->flags & FLAG_RAW)
350 return read_raw_frame(img, frame);
352 if ((img->flags & FLAG_OVERWRITE)
353 || (pub->chunk && !(frame % pub->chunk))) {
354 /* Clear the slate. */
355 img->currentframe = -1;
356 memset(img->framedata[0], 0, pub->width * pub->height);
357 memset(img->mask[0], 0, pub->width * pub->height);
359 /* Start over if we are backtracking. */
360 if (img->currentframe > frame) {
361 memset(img->mask[0], 0, pub->width * pub->height);
362 img->currentframe = -1;
365 /* We must have previous frame decoded to continue. */
366 if (frame > img->currentframe + 1) {
367 if (!lbx_img_getframe(pub, frame-1))
372 if (img->currentframe != frame) {
375 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
380 rc = _lbx_drawrow(first, img);
385 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
386 lbx_error_raise(LBX_EFORMAT);
392 img->currentframe = frame;
393 return img->framedata;
397 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
398 struct lbx_colour palette[static 256])
400 unsigned char entry[4];
403 for (i = 0; i < 256; i++) {
404 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
406 lbx_error_raise(LBX_EEOF);
411 lbx_error_raise(LBX_EFORMAT);
415 palette[i] = (struct lbx_colour) {
416 .red = entry[1] & 0x3f,
417 .green = entry[2] & 0x3f,
418 .blue = entry[3] & 0x3f,
427 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
429 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
430 unsigned char entry[4];
434 /* Do nothing if the image doesn't have embedded palette data. */
435 if (!(img->flags & FLAG_PALETTE))
438 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
442 for (i = 0; i < img->palcount; i++) {
443 rc = img->fops->read(entry, sizeof entry, img->f);
444 if (rc < sizeof entry) {
449 lbx_error_raise(LBX_EFORMAT);
453 palette[img->palstart + i] = (struct lbx_colour){
463 if (img->fops->eof(img->f))
464 lbx_error_raise(LBX_EEOF);
468 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
470 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
472 *info = (struct lbx_imginfo) {
473 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
476 /* There seems to be two ways of specifying that an image loops. */
477 if (img->flags & FLAG_LOOPING) {
480 } else if (img->leadin != pub->frames - 1) {
481 info->loopstart = img->leadin;
486 unsigned char **lbx_img_getmask(struct lbx_image *pub)
488 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
493 int lbx_img_close(struct lbx_image *pub)
495 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
499 if (img->framedata) {
500 free(img->framedata[0]);
501 free(img->framedata);
509 if (img && img->dtor) {
510 rc = img->dtor(img->f);