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;
74 /* Format constraints. */
75 if (tmp.frames <= tmp.leadin) {
76 lbx_errno = LBX_EFORMAT;
81 * DEBUG ONLY. These assertions exist to catch otherwise valid image
82 * files which differ from what I believe to be true of all LBX images.
83 * If we never find any exceptions, we can replace the assertions with
86 _lbx_assert(tmp.wtf1 == 0);
87 _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
89 new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
96 new->currentframe = -1;
98 rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
99 if (rc < new->frames+1)
101 new->foff += sizeof *new->offsets * (new->frames+1);
106 lbx_errno = LBX_EEOF;
115 static int _lbx_drawrow(int first, struct lbx_image *img)
117 uint16_t type, yval, count, xval;
122 assert(img->framedata);
125 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
126 type = letohs(type); img->foff += sizeof type;
135 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
136 yval = letohs(yval); img->foff += sizeof yval;
139 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
140 count = letohs(count); img->foff += sizeof count;
141 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
142 xval = letohs(xval); img->foff += sizeof xval;
146 /* Ensure that the row fits in the image. */
147 if (img->height - img->currenty <= yval || xval >= img->width) {
148 lbx_errno = LBX_EFORMAT;
152 img->currenty += yval;
153 img->currentx = xval;
155 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
156 xval = letohs(xval); img->foff += sizeof xval;
158 if (img->width - img->currentx <= xval) {
159 lbx_errno = LBX_EFORMAT;
162 img->currentx += xval;
168 if (count > img->width - img->currentx) {
169 lbx_errno = LBX_EFORMAT;
173 memset(&img->mask[img->currenty][img->currentx], 1, count);
175 pos = &img->framedata[img->currenty][img->currentx];
176 rc = fread(pos, 1, count, img->f);
184 if (fread(&abyss, 1, 1, img->f) != 1)
192 lbx_errno = LBX_EEOF;
199 static unsigned char **allocframebuffer(size_t width, size_t height)
201 unsigned char **new, *tmp;
204 tmp = calloc(height, width);
210 new = malloc(height * sizeof *new);
217 for (i = 0; i < height; i++) {
218 new[i] = tmp + i * width;
224 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
226 if (frame >= img->frames || frame < 0) {
227 lbx_errno = LBX_ERANGE;
231 if (!img->framedata) {
232 img->framedata = allocframebuffer(img->width, img->height);
238 img->mask = allocframebuffer(img->width, img->height);
243 /* Start over if we are backtracking. */
244 if (img->currentframe > frame)
245 img->currentframe == -1;
247 if (img->flags & FLAG_OVERWRITE) {
248 /* Clear the slate. */
249 memset(img->framedata[0], 0, img->width * img->height);
250 memset(img->mask[0], 0, img->width * img->height);
252 /* We must have previous frame decoded to continue. */
253 if (frame > img->currentframe + 1) {
254 if (!lbximg_getframe(img, frame-1))
259 if (img->currentframe != frame) {
262 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
266 rc = _lbx_drawrow(first, img);
271 if (!rc && img->foff > img->offsets[frame+1]) {
272 lbx_errno = LBX_EFORMAT;
278 img->currentframe = frame;
279 return img->framedata;
282 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
287 for (i = 0; i < 256; i++) {
288 if (fread(entry, sizeof entry, 1, f) != 1) {
289 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
294 lbx_errno = LBX_EFORMAT;
298 palette[i] = (struct lbx_colour){
299 .red = entry[1] << 2,
300 .green = entry[2] << 2,
301 .blue = entry[3] << 2,
309 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
311 size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
315 uint16_t start, count;
318 /* Do nothing if the image doesn't have embedded palette data. */
319 if (!(img->flags & FLAG_PALETTE))
322 /* Palette data is located right after the header. */
323 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
327 if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
328 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
329 start = letohs(start); img->foff += sizeof start;
330 count = letohs(count); img->foff += sizeof count;
332 if (start + count > 256) {
333 lbx_errno = LBX_EFORMAT;
337 if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
338 lbx_errno = LBX_EFORMAT;
342 for (i = 0; i < count; i++) {
343 rc = fread(entry, 1, sizeof entry, img->f);
346 if (rc < sizeof entry) {
351 lbx_errno = LBX_EFORMAT;
355 palette[start + i] = (struct lbx_colour){
356 .red = entry[1] << 2,
357 .green = entry[2] << 2,
358 .blue = entry[3] << 2,
364 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
368 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
370 *info = (struct lbx_imginfo) {
372 .height = img->height,
373 .nframes = img->frames,
374 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
377 /* There seems to be two ways of specifying that an image loops. */
378 if (img->flags & FLAG_LOOPING) {
381 } else if (img->leadin != img->frames - 1) {
382 info->loopstart = img->leadin;
387 unsigned char **lbximg_getmask(struct lbx_image *img)
392 void lbximg_close(struct lbx_image *img)
396 if (img->framedata) {
397 free(img->framedata[0]);
398 free(img->framedata);