2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright (C) 2006-2008 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/>.
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_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
43 unsigned short width, height;
44 unsigned short wtf, flags;
45 unsigned short frames, 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 *lbximg_init(unsigned char hdr[static HDR_LEN])
64 unsigned short nframes = unpack_16_le(hdr+6);
65 struct lbx_image *img;
67 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
73 *img = (struct lbx_image) {
74 .width = unpack_16_le(hdr+0),
75 .height = unpack_16_le(hdr+2),
76 .wtf = unpack_16_le(hdr+4),
77 .frames = unpack_16_le(hdr+6),
78 .leadin = unpack_16_le(hdr+8),
79 .flags = unpack_16_le(hdr+10),
87 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
88 int (*destructor)(void *))
90 unsigned char hdr_buf[HDR_LEN];
91 struct lbx_image *img;
93 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
100 img = lbximg_init(hdr_buf);
106 img->dtor = destructor;
109 * DEBUG ONLY. These assertions exist to catch otherwise valid image
110 * files which differ from what I believe to be true of all LBX images.
111 * If we never find any exceptions, we can replace the assertions with
114 _lbx_assert(img->wtf == 0); /* version? */
115 _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
116 _lbx_assert(!(img->flags & ~FLAG_ALL));
118 /* Read all offsets. Should be merged with identical code in lbx.c */
119 for (unsigned i = 0; i <= img->frames; i++) {
120 unsigned char buf[4];
122 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
125 lbx_errno = LBX_EEOF;
130 img->offsets[i] = unpack_32_le(buf);
133 if (img->flags & FLAG_PALETTE) {
134 unsigned char buf[4];
136 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
139 lbx_errno = 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_errno = LBX_EFORMAT;
158 struct lbx_image *lbximg_fopen(FILE *f)
160 return lbximg_open(f, &lbx_default_fops, NULL);
163 static int _lbx_drawrow(int first, struct lbx_image *img)
165 unsigned short type, count, yval, xval;
166 unsigned char buf[4];
170 assert(img->framedata);
173 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
175 type = unpack_16_le(buf+0);
184 yval = unpack_16_le(buf+2);
188 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
190 count = unpack_16_le(buf+0);
192 xval = unpack_16_le(buf+2);
196 /* Ensure that the row fits in the image. */
197 if (img->height - img->currenty <= yval || xval >= img->width) {
198 lbx_errno = LBX_EFORMAT;
202 img->currenty += yval;
203 img->currentx = xval;
205 xval = unpack_16_le(buf+2);
207 if (img->width - img->currentx <= xval) {
208 lbx_errno = LBX_EFORMAT;
211 img->currentx += xval;
216 if (count > img->width - img->currentx) {
217 lbx_errno = LBX_EFORMAT;
221 memset(&img->mask[img->currenty][img->currentx], 1, count);
223 pos = &img->framedata[img->currenty][img->currentx];
224 rc = img->fops->read(pos, count, img->f);
231 if (img->fops->read(buf, 1, img->f) != 1)
238 if (img->fops->eof(img->f))
239 lbx_errno = LBX_EEOF;
243 static unsigned char **allocframebuffer(size_t width, size_t height)
245 unsigned char **new, *tmp;
248 tmp = calloc(height, width);
254 new = malloc(height * sizeof *new);
261 for (i = 0; i < height; i++) {
262 new[i] = tmp + i * width;
268 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
270 if (frame >= img->frames || frame < 0) {
271 lbx_errno = LBX_ERANGE;
275 if (!img->framedata) {
276 img->framedata = allocframebuffer(img->width, img->height);
282 img->mask = allocframebuffer(img->width, img->height);
287 if (img->flags & FLAG_OVERWRITE) {
288 /* Clear the slate. */
289 img->currentframe = -1;
290 memset(img->framedata[0], 0, img->width * img->height);
291 memset(img->mask[0], 0, img->width * img->height);
293 /* Start over if we are backtracking. */
294 if (img->currentframe > frame)
295 img->currentframe = -1;
297 /* We must have previous frame decoded to continue. */
298 if (frame > img->currentframe + 1) {
299 if (!lbximg_getframe(img, frame-1))
304 if (img->currentframe != frame) {
307 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
313 rc = _lbx_drawrow(first, img);
318 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
319 lbx_errno = LBX_EFORMAT;
325 img->currentframe = frame;
326 return img->framedata;
330 lbximg_loadpalette(void *f, const struct lbx_file_ops *fops,
331 struct lbx_colour palette[static 256])
333 unsigned char entry[4];
336 for (i = 0; i < 256; i++) {
337 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
338 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
343 lbx_errno = LBX_EFORMAT;
347 palette[i] = (struct lbx_colour) {
348 .red = entry[1] << 2,
349 .green = entry[2] << 2,
350 .blue = entry[3] << 2,
359 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
361 unsigned char entry[4];
365 /* Do nothing if the image doesn't have embedded palette data. */
366 if (!(img->flags & FLAG_PALETTE))
369 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
374 for (i = 0; i < img->palcount; i++) {
375 rc = img->fops->read(entry, sizeof entry, img->f);
376 if (rc < sizeof entry) {
381 lbx_errno = LBX_EFORMAT;
385 palette[img->palstart + i] = (struct lbx_colour){
386 .red = entry[1] << 2,
387 .green = entry[2] << 2,
388 .blue = entry[3] << 2,
395 lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
399 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
401 *info = (struct lbx_imginfo) {
403 .height = img->height,
404 .nframes = img->frames,
405 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
408 /* There seems to be two ways of specifying that an image loops. */
409 if (img->flags & FLAG_LOOPING) {
412 } else if (img->leadin != img->frames - 1) {
413 info->loopstart = img->leadin;
418 unsigned char **lbximg_getmask(struct lbx_image *img)
423 int lbximg_close(struct lbx_image *img)
428 if (img->framedata) {
429 free(img->framedata[0]);
430 free(img->framedata);
438 if (img && img->dtor) {
439 rc = img->dtor(img->f);