1 /* 2ooM: The Master of Orion II Reverse Engineering Project
2 * Library for working with LBX image files.
3 * Copyright (C) 2006-2008 Nick Bowler
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
29 #include "byteorder.h"
34 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
35 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
36 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
39 uint16_t width, height;
41 uint16_t frames, leadin;
43 uint16_t palstart, palcount;
49 int currentx, currenty;
50 unsigned char **framedata;
56 struct lbx_image *lbximg_fopen(FILE *f)
58 struct lbx_image tmp = {.f = f}, *new = NULL;
61 if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
62 if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
63 if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr;
64 if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr;
65 if (fread(&tmp.leadin, sizeof tmp.leadin, 1, f) != 1) goto readerr;
66 if (fread(&tmp.flags, sizeof tmp.flags, 1, f) != 1) goto readerr;
68 tmp.width = letohs(tmp.width); tmp.foff += sizeof tmp.width;
69 tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
70 tmp.wtf1 = letohs(tmp.wtf1); tmp.foff += sizeof tmp.wtf1;
71 tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
72 tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
73 tmp.flags = letohs(tmp.flags); tmp.foff += sizeof tmp.flags;
76 * DEBUG ONLY. These assertions exist to catch otherwise valid image
77 * files which differ from what I believe to be true of all LBX images.
78 * If we never find any exceptions, we can replace the assertions with
81 _lbx_assert(tmp.wtf1 == 0);
82 _lbx_assert(tmp.frames > tmp.leadin); /* cmbtshp.lbx breaks this. */
83 _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
85 new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
92 new->currentframe = -1;
94 rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
95 if (rc < new->frames+1)
97 new->foff += sizeof *new->offsets * (new->frames+1);
99 if (new->flags & FLAG_PALETTE) {
100 if (fread(&new->palstart, sizeof new->palstart, 1, f) != 1)
102 if (fread(&new->palcount, sizeof new->palcount, 1, f) != 1)
105 new->palstart = letohs(new->palstart);
106 new->palcount = letohs(new->palcount);
107 new->foff += sizeof new->palstart + sizeof new->palcount;
108 new->paloff = new->foff;
110 if (new->palstart + new->palcount > 256) {
111 lbx_errno = LBX_EFORMAT;
120 lbx_errno = LBX_EEOF;
129 static int _lbx_drawrow(int first, struct lbx_image *img)
131 uint16_t type, yval, count, xval;
136 assert(img->framedata);
139 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
140 type = letohs(type); img->foff += sizeof type;
149 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
150 yval = letohs(yval); img->foff += sizeof yval;
153 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
154 count = letohs(count); img->foff += sizeof count;
155 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
156 xval = letohs(xval); img->foff += sizeof xval;
160 /* Ensure that the row fits in the image. */
161 if (img->height - img->currenty <= yval || xval >= img->width) {
162 lbx_errno = LBX_EFORMAT;
166 img->currenty += yval;
167 img->currentx = xval;
169 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
170 xval = letohs(xval); img->foff += sizeof xval;
172 if (img->width - img->currentx <= xval) {
173 lbx_errno = LBX_EFORMAT;
176 img->currentx += xval;
182 if (count > img->width - img->currentx) {
183 lbx_errno = LBX_EFORMAT;
187 memset(&img->mask[img->currenty][img->currentx], 1, count);
189 pos = &img->framedata[img->currenty][img->currentx];
190 rc = fread(pos, 1, count, img->f);
198 if (fread(&abyss, 1, 1, img->f) != 1)
206 lbx_errno = LBX_EEOF;
213 static unsigned char **allocframebuffer(size_t width, size_t height)
215 unsigned char **new, *tmp;
218 tmp = calloc(height, width);
224 new = malloc(height * sizeof *new);
231 for (i = 0; i < height; i++) {
232 new[i] = tmp + i * width;
238 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
240 if (frame >= img->frames || frame < 0) {
241 lbx_errno = LBX_ERANGE;
245 if (!img->framedata) {
246 img->framedata = allocframebuffer(img->width, img->height);
252 img->mask = allocframebuffer(img->width, img->height);
257 /* Start over if we are backtracking. */
258 if (img->currentframe > frame)
259 img->currentframe == -1;
261 if (img->flags & FLAG_OVERWRITE) {
262 /* Clear the slate. */
263 memset(img->framedata[0], 0, img->width * img->height);
264 memset(img->mask[0], 0, img->width * img->height);
266 /* We must have previous frame decoded to continue. */
267 if (frame > img->currentframe + 1) {
268 if (!lbximg_getframe(img, frame-1))
273 if (img->currentframe != frame) {
276 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
280 rc = _lbx_drawrow(first, img);
285 if (!rc && img->foff > img->offsets[frame+1]) {
286 lbx_errno = LBX_EFORMAT;
292 img->currentframe = frame;
293 return img->framedata;
296 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
301 for (i = 0; i < 256; i++) {
302 if (fread(entry, sizeof entry, 1, f) != 1) {
303 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
308 lbx_errno = LBX_EFORMAT;
312 palette[i] = (struct lbx_colour){
313 .red = entry[1] << 2,
314 .green = entry[2] << 2,
315 .blue = entry[3] << 2,
323 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
330 /* Do nothing if the image doesn't have embedded palette data. */
331 if (!(img->flags & FLAG_PALETTE))
334 if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
337 for (i = 0; i < img->palcount; i++) {
338 rc = fread(entry, 1, sizeof entry, img->f);
341 if (rc < sizeof entry) {
346 lbx_errno = LBX_EFORMAT;
350 palette[img->palstart + i] = (struct lbx_colour){
351 .red = entry[1] << 2,
352 .green = entry[2] << 2,
353 .blue = entry[3] << 2,
359 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
363 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
365 *info = (struct lbx_imginfo) {
367 .height = img->height,
368 .nframes = img->frames,
369 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
372 /* There seems to be two ways of specifying that an image loops. */
373 if (img->flags & FLAG_LOOPING) {
376 } else if (img->leadin != img->frames - 1) {
377 info->loopstart = img->leadin;
382 unsigned char **lbximg_getmask(struct lbx_image *img)
387 void lbximg_close(struct lbx_image *img)
391 if (img->framedata) {
392 free(img->framedata[0]);
393 free(img->framedata);