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;
48 int currentx, currenty;
49 unsigned char **framedata;
55 struct lbx_image *lbximg_fopen(FILE *f)
57 struct lbx_image tmp = {.f = f}, *new = NULL;
60 if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
61 if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
62 if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr;
63 if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr;
64 if (fread(&tmp.leadin, sizeof tmp.leadin, 1, f) != 1) goto readerr;
65 if (fread(&tmp.flags, sizeof tmp.flags, 1, f) != 1) goto readerr;
67 tmp.width = letohs(tmp.width); tmp.foff += sizeof tmp.width;
68 tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
69 tmp.wtf1 = letohs(tmp.wtf1); tmp.foff += sizeof tmp.wtf1;
70 tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
71 tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
72 tmp.flags = letohs(tmp.flags); tmp.foff += sizeof tmp.flags;
75 * DEBUG ONLY. These assertions exist to catch otherwise valid image
76 * files which differ from what I believe to be true of all LBX images.
77 * If we never find any exceptions, we can replace the assertions with
80 _lbx_assert(tmp.wtf1 == 0);
81 _lbx_assert(tmp.frames > tmp.leadin); /* cmbtshp.lbx breaks this. */
82 _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
84 new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
91 new->currentframe = -1;
93 rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
94 if (rc < new->frames+1)
96 new->foff += sizeof *new->offsets * (new->frames+1);
101 lbx_errno = LBX_EEOF;
110 static int _lbx_drawrow(int first, struct lbx_image *img)
112 uint16_t type, yval, count, xval;
117 assert(img->framedata);
120 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
121 type = letohs(type); img->foff += sizeof type;
130 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
131 yval = letohs(yval); img->foff += sizeof yval;
134 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
135 count = letohs(count); img->foff += sizeof count;
136 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
137 xval = letohs(xval); img->foff += sizeof xval;
141 /* Ensure that the row fits in the image. */
142 if (img->height - img->currenty <= yval || xval >= img->width) {
143 lbx_errno = LBX_EFORMAT;
147 img->currenty += yval;
148 img->currentx = xval;
150 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
151 xval = letohs(xval); img->foff += sizeof xval;
153 if (img->width - img->currentx <= xval) {
154 lbx_errno = LBX_EFORMAT;
157 img->currentx += xval;
163 if (count > img->width - img->currentx) {
164 lbx_errno = LBX_EFORMAT;
168 memset(&img->mask[img->currenty][img->currentx], 1, count);
170 pos = &img->framedata[img->currenty][img->currentx];
171 rc = fread(pos, 1, count, img->f);
179 if (fread(&abyss, 1, 1, img->f) != 1)
187 lbx_errno = LBX_EEOF;
194 static unsigned char **allocframebuffer(size_t width, size_t height)
196 unsigned char **new, *tmp;
199 tmp = calloc(height, width);
205 new = malloc(height * sizeof *new);
212 for (i = 0; i < height; i++) {
213 new[i] = tmp + i * width;
219 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
221 if (frame >= img->frames || frame < 0) {
222 lbx_errno = LBX_ERANGE;
226 if (!img->framedata) {
227 img->framedata = allocframebuffer(img->width, img->height);
233 img->mask = allocframebuffer(img->width, img->height);
238 /* Start over if we are backtracking. */
239 if (img->currentframe > frame)
240 img->currentframe == -1;
242 if (img->flags & FLAG_OVERWRITE) {
243 /* Clear the slate. */
244 memset(img->framedata[0], 0, img->width * img->height);
245 memset(img->mask[0], 0, img->width * img->height);
247 /* We must have previous frame decoded to continue. */
248 if (frame > img->currentframe + 1) {
249 if (!lbximg_getframe(img, frame-1))
254 if (img->currentframe != frame) {
257 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
261 rc = _lbx_drawrow(first, img);
266 if (!rc && img->foff > img->offsets[frame+1]) {
267 lbx_errno = LBX_EFORMAT;
273 img->currentframe = frame;
274 return img->framedata;
277 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
282 for (i = 0; i < 256; i++) {
283 if (fread(entry, sizeof entry, 1, f) != 1) {
284 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
289 lbx_errno = LBX_EFORMAT;
293 palette[i] = (struct lbx_colour){
294 .red = entry[1] << 2,
295 .green = entry[2] << 2,
296 .blue = entry[3] << 2,
304 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
306 size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
310 uint16_t start, count;
313 /* Do nothing if the image doesn't have embedded palette data. */
314 if (!(img->flags & FLAG_PALETTE))
317 /* Palette data is located right after the header. */
318 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
322 if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
323 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
324 start = letohs(start); img->foff += sizeof start;
325 count = letohs(count); img->foff += sizeof count;
327 if (start + count > 256) {
328 lbx_errno = LBX_EFORMAT;
332 if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
333 lbx_errno = LBX_EFORMAT;
337 for (i = 0; i < count; i++) {
338 rc = fread(entry, 1, sizeof entry, img->f);
341 if (rc < sizeof entry) {
346 lbx_errno = LBX_EFORMAT;
350 palette[start + 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 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
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);