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/>.
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;
46 unsigned char wtf2, leadin;
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),
90 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
91 int (*destructor)(void *))
93 unsigned char hdr_buf[HDR_LEN];
94 struct lbx_image_priv *img;
96 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
98 lbx_error_raise(LBX_EEOF);
102 img = lbx_img_init(hdr_buf);
108 img->dtor = destructor;
111 * DEBUG ONLY. These assertions exist to catch otherwise valid image
112 * files which differ from what I believe to be true of all LBX images.
113 * When we can decode every image, then these assertions should be
114 * replaced with constraints.
116 _lbx_assert(img->wtf == 0); /* version? */
117 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
118 _lbx_assert(img->pub.frames > img->leadin);
119 _lbx_assert(!(img->flags & ~FLAG_ALL));
121 /* Read all offsets. Should be merged with identical code in lbx.c */
122 for (unsigned i = 0; i <= img->pub.frames; i++) {
123 unsigned char buf[4];
125 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
127 lbx_error_raise(LBX_EEOF);
132 img->offsets[i] = unpack_32_le(buf);
135 if (img->flags & FLAG_PALETTE) {
136 unsigned char buf[4];
138 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
140 lbx_error_raise(LBX_EEOF);
145 img->palstart = unpack_16_le(buf+0);
146 img->palcount = unpack_16_le(buf+2);
147 img->paloff = fops->tell(f);
149 if (img->palstart + img->palcount > 256) {
150 lbx_error_raise(LBX_EFORMAT);
159 static int pipe_close(void *f)
161 struct lbx_pipe_state *p = f;
169 static int file_close(void *f)
171 return fclose((FILE *)f);
174 struct lbx_image *lbx_img_fopen(const char *file)
176 struct lbx_pipe_state *p;
179 f = fopen(file, "rb");
181 lbx_error_raise(-errno);
185 if (fseek(f, 0, SEEK_CUR) == 0)
186 return lbx_img_open(f, &lbx_default_fops, file_close);
188 p = malloc(sizeof *p);
190 lbx_error_raise(LBX_ENOMEM);
195 *p = (struct lbx_pipe_state) { .f = f };
196 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
199 static int _lbx_drawrow(struct lbx_image_priv *img)
201 unsigned short length, offset;
202 unsigned char buf[4];
206 assert(img->framedata);
209 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
212 length = unpack_16_le(buf+0);
213 offset = unpack_16_le(buf+2);
214 if (length == 0 && offset == 1000)
217 /* Length of 0 increments Y position */
219 if (offset > img->pub.height - img->currenty) {
220 lbx_error_raise(LBX_EFORMAT);
224 img->currenty += offset;
229 /* Otherwise we read pixel data */
230 if (offset > img->pub.width - img->currentx) {
231 lbx_error_raise(LBX_EFORMAT);
234 img->currentx += offset;
236 if (length > img->pub.width - img->currentx) {
237 lbx_error_raise(LBX_EFORMAT);
241 memset(&img->mask[img->currenty][img->currentx], 1, length);
243 pos = &img->framedata[img->currenty][img->currentx];
244 rc = img->fops->read(pos, length, img->f);
250 /* Skip padding byte, if any */
252 if (img->fops->read(buf, 1, img->f) != 1)
258 if (img->fops->eof(img->f))
259 lbx_error_raise(LBX_EEOF);
263 static unsigned char **allocframebuffer(size_t width, size_t height)
265 unsigned char **new, *tmp;
268 if (height > SIZE_MAX / sizeof *new) {
269 lbx_error_raise(LBX_ENOMEM);
273 /* Ensure that there is at least one row in the framebuffer. */
274 if (height == 0 || width == 0)
277 tmp = calloc(height, width);
279 lbx_error_raise(LBX_ENOMEM);
283 new = malloc(height * sizeof *new);
285 lbx_error_raise(LBX_ENOMEM);
290 for (i = 0; i < height; i++) {
291 new[i] = tmp + i * width;
297 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
299 unsigned long size = img->pub.width * img->pub.height;
301 assert(img->flags & FLAG_RAW);
303 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
307 if (img->fops->read(img->framedata[0], size, img->f) != size) {
308 if (img->fops->eof(img->f))
309 lbx_error_raise(LBX_EEOF);
312 memset(img->mask[0], 1, size);
314 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
315 lbx_error_raise(LBX_EFORMAT);
319 return img->framedata;
322 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
324 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
325 unsigned char buf[4];
327 if (frame >= pub->frames || frame < 0) {
328 lbx_error_raise(LBX_ENOENT);
332 if (!img->framedata) {
333 img->framedata = allocframebuffer(pub->width, pub->height);
339 img->mask = allocframebuffer(pub->width, pub->height);
344 if (img->flags & FLAG_RAW)
345 return read_raw_frame(img, frame);
347 if ((img->flags & FLAG_OVERWRITE)
348 || (pub->chunk && !(frame % pub->chunk))) {
349 /* Clear the slate. */
350 img->currentframe = -1;
351 memset(img->framedata[0], 0, pub->width * pub->height);
352 memset(img->mask[0], 0, pub->width * pub->height);
354 /* Start over if we are backtracking. */
355 if (img->currentframe > frame) {
356 memset(img->mask[0], 0, pub->width * pub->height);
357 img->currentframe = -1;
360 /* We must have previous frame decoded to continue. */
361 if (frame > img->currentframe + 1) {
362 if (!lbx_img_getframe(pub, frame-1))
367 if (img->currentframe != frame) {
370 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
374 /* Read frame header */
375 if (img->fops->read(buf, 4, img->f) != 4) {
376 if (img->fops->eof(img->f))
377 lbx_error_raise(LBX_EEOF);
381 if (unpack_16_le(buf) != 1) {
382 lbx_error_raise(LBX_EFORMAT);
387 img->currenty = unpack_16_le(buf+2);
388 if (img->currenty > img->pub.height) {
389 lbx_error_raise(LBX_EFORMAT);
394 rc = _lbx_drawrow(img);
398 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
399 lbx_error_raise(LBX_EFORMAT);
405 img->currentframe = frame;
406 return img->framedata;
410 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
411 struct lbx_colour palette[static 256])
413 unsigned char entry[4];
416 for (i = 0; i < 256; i++) {
417 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
419 lbx_error_raise(LBX_EEOF);
424 lbx_error_raise(LBX_EFORMAT);
428 palette[i] = (struct lbx_colour) {
429 .red = entry[1] & 0x3f,
430 .green = entry[2] & 0x3f,
431 .blue = entry[3] & 0x3f,
440 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
442 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
443 unsigned char entry[4];
447 /* Do nothing if the image doesn't have embedded palette data. */
448 if (!(img->flags & FLAG_PALETTE))
451 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
455 for (i = 0; i < img->palcount; i++) {
456 rc = img->fops->read(entry, sizeof entry, img->f);
457 if (rc < sizeof entry) {
462 lbx_error_raise(LBX_EFORMAT);
466 palette[img->palstart + i] = (struct lbx_colour){
476 if (img->fops->eof(img->f))
477 lbx_error_raise(LBX_EEOF);
481 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
483 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
485 *info = (struct lbx_imginfo) {
486 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
489 /* There seems to be two ways of specifying that an image loops. */
490 if (img->flags & FLAG_LOOPING) {
493 } else if (img->leadin != pub->frames - 1) {
494 info->loopstart = img->leadin;
499 unsigned char **lbx_img_getmask(struct lbx_image *pub)
501 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
506 int lbx_img_close(struct lbx_image *pub)
508 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
512 if (img->framedata) {
513 free(img->framedata[0]);
514 free(img->framedata);
522 if (img && img->dtor) {
523 rc = img->dtor(img->f);