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(int first, struct lbx_image_priv *img)
201 unsigned short type, count, yval, xval;
202 unsigned char buf[4];
206 assert(img->framedata);
209 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
211 type = unpack_16_le(buf+0);
220 yval = unpack_16_le(buf+2);
224 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
226 count = unpack_16_le(buf+0);
228 xval = unpack_16_le(buf+2);
232 /* Ensure that the row fits in the image. */
233 if (img->pub.height - img->currenty <= yval
234 || xval >= img->pub.width) {
235 lbx_error_raise(LBX_EFORMAT);
239 img->currenty += yval;
240 img->currentx = xval;
242 xval = unpack_16_le(buf+2);
244 if (img->pub.width - img->currentx <= xval) {
245 lbx_error_raise(LBX_EFORMAT);
248 img->currentx += xval;
253 if (count > img->pub.width - img->currentx) {
254 lbx_error_raise(LBX_EFORMAT);
258 memset(&img->mask[img->currenty][img->currentx], 1, count);
260 pos = &img->framedata[img->currenty][img->currentx];
261 rc = img->fops->read(pos, count, img->f);
268 if (img->fops->read(buf, 1, img->f) != 1)
274 if (img->fops->eof(img->f))
275 lbx_error_raise(LBX_EEOF);
279 static unsigned char **allocframebuffer(size_t width, size_t height)
281 unsigned char **new, *tmp;
284 if (height > SIZE_MAX / sizeof *new) {
285 lbx_error_raise(LBX_ENOMEM);
289 /* Ensure that there is at least one row in the framebuffer. */
290 if (height == 0 || width == 0)
293 tmp = calloc(height, width);
295 lbx_error_raise(LBX_ENOMEM);
299 new = malloc(height * sizeof *new);
301 lbx_error_raise(LBX_ENOMEM);
306 for (i = 0; i < height; i++) {
307 new[i] = tmp + i * width;
313 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
315 unsigned long size = img->pub.width * img->pub.height;
317 assert(img->flags & FLAG_RAW);
319 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
323 if (img->fops->read(img->framedata[0], size, img->f) != size) {
324 if (img->fops->eof(img->f))
325 lbx_error_raise(LBX_EEOF);
328 memset(img->mask[0], 1, size);
330 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
331 lbx_error_raise(LBX_EFORMAT);
335 return img->framedata;
338 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
340 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
342 if (frame >= pub->frames || frame < 0) {
343 lbx_error_raise(LBX_ENOENT);
347 if (!img->framedata) {
348 img->framedata = allocframebuffer(pub->width, pub->height);
354 img->mask = allocframebuffer(pub->width, pub->height);
359 if (img->flags & FLAG_RAW)
360 return read_raw_frame(img, frame);
362 if ((img->flags & FLAG_OVERWRITE)
363 || (pub->chunk && !(frame % pub->chunk))) {
364 /* Clear the slate. */
365 img->currentframe = -1;
366 memset(img->framedata[0], 0, pub->width * pub->height);
367 memset(img->mask[0], 0, pub->width * pub->height);
369 /* Start over if we are backtracking. */
370 if (img->currentframe > frame) {
371 memset(img->mask[0], 0, pub->width * pub->height);
372 img->currentframe = -1;
375 /* We must have previous frame decoded to continue. */
376 if (frame > img->currentframe + 1) {
377 if (!lbx_img_getframe(pub, frame-1))
382 if (img->currentframe != frame) {
385 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
390 rc = _lbx_drawrow(first, img);
395 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
396 lbx_error_raise(LBX_EFORMAT);
402 img->currentframe = frame;
403 return img->framedata;
407 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
408 struct lbx_colour palette[static 256])
410 unsigned char entry[4];
413 for (i = 0; i < 256; i++) {
414 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
416 lbx_error_raise(LBX_EEOF);
421 lbx_error_raise(LBX_EFORMAT);
425 palette[i] = (struct lbx_colour) {
426 .red = entry[1] & 0x3f,
427 .green = entry[2] & 0x3f,
428 .blue = entry[3] & 0x3f,
437 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
439 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
440 unsigned char entry[4];
444 /* Do nothing if the image doesn't have embedded palette data. */
445 if (!(img->flags & FLAG_PALETTE))
448 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
452 for (i = 0; i < img->palcount; i++) {
453 rc = img->fops->read(entry, sizeof entry, img->f);
454 if (rc < sizeof entry) {
459 lbx_error_raise(LBX_EFORMAT);
463 palette[img->palstart + i] = (struct lbx_colour){
473 if (img->fops->eof(img->f))
474 lbx_error_raise(LBX_EEOF);
478 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
480 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
482 *info = (struct lbx_imginfo) {
483 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
486 /* There seems to be two ways of specifying that an image loops. */
487 if (img->flags & FLAG_LOOPING) {
490 } else if (img->leadin != pub->frames - 1) {
491 info->loopstart = img->leadin;
496 unsigned char **lbx_img_getmask(struct lbx_image *pub)
498 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
503 int lbx_img_close(struct lbx_image *pub)
505 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
509 if (img->framedata) {
510 free(img->framedata[0]);
511 free(img->framedata);
519 if (img && img->dtor) {
520 rc = img->dtor(img->f);