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/>.
29 #include "byteorder.h"
35 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
36 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
37 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
38 #define FLAG_ALL (FLAG_OVERWRITE|FLAG_PALETTE|FLAG_LOOPING)
43 unsigned short width, height;
44 unsigned short wtf, flags;
45 unsigned short frames, leadin;
46 unsigned short palstart, palcount;
52 int currentx, currenty;
53 unsigned char **framedata;
56 unsigned long offsets[];
59 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
61 unsigned short nframes = unpack_16_le(hdr+6);
62 struct lbx_image *img;
64 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
70 *img = (struct lbx_image) {
71 .width = unpack_16_le(hdr+0),
72 .height = unpack_16_le(hdr+2),
73 .wtf = unpack_16_le(hdr+4),
74 .frames = unpack_16_le(hdr+6),
75 .leadin = unpack_16_le(hdr+8),
76 .flags = unpack_16_le(hdr+10),
84 struct lbx_image *lbximg_fopen(FILE *f)
86 unsigned char hdr_buf[HDR_LEN];
87 struct lbx_image *img;
90 if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
97 img = lbximg_init(hdr_buf);
102 img->foff = sizeof hdr_buf;
105 * DEBUG ONLY. These assertions exist to catch otherwise valid image
106 * files which differ from what I believe to be true of all LBX images.
107 * If we never find any exceptions, we can replace the assertions with
110 _lbx_assert(img->wtf == 0); /* version? */
111 _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
112 _lbx_assert(!(img->flags & ~FLAG_ALL));
114 /* Read all offsets. Should be merged with identical code in lbx.c */
115 for (unsigned i = 0; i <= img->frames; i++) {
116 unsigned char buf[4];
118 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
121 lbx_errno = LBX_EEOF;
126 img->offsets[i] = unpack_32_le(buf);
130 if (img->flags & FLAG_PALETTE) {
131 unsigned char buf[4];
133 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
136 lbx_errno = LBX_EEOF;
141 img->palstart = unpack_16_le(buf+0);
142 img->palcount = unpack_16_le(buf+2);
143 img->foff += sizeof buf;
144 img->paloff = img->foff;
146 if (img->palstart + img->palcount > 256) {
147 lbx_errno = LBX_EFORMAT;
156 static int _lbx_drawrow(int first, struct lbx_image *img)
158 uint16_t type, yval, count, xval;
163 assert(img->framedata);
166 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
167 type = letohs(type); img->foff += sizeof type;
176 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
177 yval = letohs(yval); img->foff += sizeof yval;
180 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
181 count = letohs(count); img->foff += sizeof count;
182 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
183 xval = letohs(xval); img->foff += sizeof xval;
187 /* Ensure that the row fits in the image. */
188 if (img->height - img->currenty <= yval || xval >= img->width) {
189 lbx_errno = LBX_EFORMAT;
193 img->currenty += yval;
194 img->currentx = xval;
196 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
197 xval = letohs(xval); img->foff += sizeof xval;
199 if (img->width - img->currentx <= xval) {
200 lbx_errno = LBX_EFORMAT;
203 img->currentx += xval;
209 if (count > img->width - img->currentx) {
210 lbx_errno = LBX_EFORMAT;
214 memset(&img->mask[img->currenty][img->currentx], 1, count);
216 pos = &img->framedata[img->currenty][img->currentx];
217 rc = fread(pos, 1, count, img->f);
225 if (fread(&abyss, 1, 1, img->f) != 1)
233 lbx_errno = LBX_EEOF;
240 static unsigned char **allocframebuffer(size_t width, size_t height)
242 unsigned char **new, *tmp;
245 tmp = calloc(height, width);
251 new = malloc(height * sizeof *new);
258 for (i = 0; i < height; i++) {
259 new[i] = tmp + i * width;
265 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
267 if (frame >= img->frames || frame < 0) {
268 lbx_errno = LBX_ERANGE;
272 if (!img->framedata) {
273 img->framedata = allocframebuffer(img->width, img->height);
279 img->mask = allocframebuffer(img->width, img->height);
284 /* Start over if we are backtracking. */
285 if (img->currentframe > frame)
286 img->currentframe == -1;
288 if (img->flags & FLAG_OVERWRITE) {
289 /* Clear the slate. */
290 memset(img->framedata[0], 0, img->width * img->height);
291 memset(img->mask[0], 0, img->width * img->height);
293 /* We must have previous frame decoded to continue. */
294 if (frame > img->currentframe + 1) {
295 if (!lbximg_getframe(img, frame-1))
300 if (img->currentframe != frame) {
303 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
307 rc = _lbx_drawrow(first, img);
312 if (!rc && img->foff > img->offsets[frame+1]) {
313 lbx_errno = LBX_EFORMAT;
319 img->currentframe = frame;
320 return img->framedata;
323 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
328 for (i = 0; i < 256; i++) {
329 if (fread(entry, sizeof entry, 1, f) != 1) {
330 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
335 lbx_errno = LBX_EFORMAT;
339 palette[i] = (struct lbx_colour){
340 .red = entry[1] << 2,
341 .green = entry[2] << 2,
342 .blue = entry[3] << 2,
350 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
357 /* Do nothing if the image doesn't have embedded palette data. */
358 if (!(img->flags & FLAG_PALETTE))
361 if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
364 for (i = 0; i < img->palcount; i++) {
365 rc = fread(entry, 1, sizeof entry, img->f);
368 if (rc < sizeof entry) {
373 lbx_errno = LBX_EFORMAT;
377 palette[img->palstart + i] = (struct lbx_colour){
378 .red = entry[1] << 2,
379 .green = entry[2] << 2,
380 .blue = entry[3] << 2,
386 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
390 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
392 *info = (struct lbx_imginfo) {
394 .height = img->height,
395 .nframes = img->frames,
396 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
399 /* There seems to be two ways of specifying that an image loops. */
400 if (img->flags & FLAG_LOOPING) {
403 } else if (img->leadin != img->frames - 1) {
404 info->loopstart = img->leadin;
409 unsigned char **lbximg_getmask(struct lbx_image *img)
414 void lbximg_close(struct lbx_image *img)
418 if (img->framedata) {
419 free(img->framedata[0]);
420 free(img->framedata);