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/>.
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). */
37 #define FLAG_ALL (FLAG_OVERWRITE|FLAG_PALETTE|FLAG_LOOPING)
42 unsigned short width, height;
43 unsigned short wtf, flags;
44 unsigned short frames, leadin;
45 unsigned short palstart, palcount;
51 int currentx, currenty;
52 unsigned char **framedata;
55 unsigned long offsets[];
58 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
60 unsigned short nframes = unpack_16_le(hdr+6);
61 struct lbx_image *img;
63 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
69 *img = (struct lbx_image) {
70 .width = unpack_16_le(hdr+0),
71 .height = unpack_16_le(hdr+2),
72 .wtf = unpack_16_le(hdr+4),
73 .frames = unpack_16_le(hdr+6),
74 .leadin = unpack_16_le(hdr+8),
75 .flags = unpack_16_le(hdr+10),
83 struct lbx_image *lbximg_fopen(FILE *f)
85 unsigned char hdr_buf[HDR_LEN];
86 struct lbx_image *img;
89 if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
96 img = lbximg_init(hdr_buf);
101 img->foff = sizeof hdr_buf;
104 * DEBUG ONLY. These assertions exist to catch otherwise valid image
105 * files which differ from what I believe to be true of all LBX images.
106 * If we never find any exceptions, we can replace the assertions with
109 _lbx_assert(img->wtf == 0); /* version? */
110 _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
111 _lbx_assert(!(img->flags & ~FLAG_ALL));
113 /* Read all offsets. Should be merged with identical code in lbx.c */
114 for (unsigned i = 0; i <= img->frames; i++) {
115 unsigned char buf[4];
117 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
120 lbx_errno = LBX_EEOF;
125 img->offsets[i] = unpack_32_le(buf);
129 if (img->flags & FLAG_PALETTE) {
130 unsigned char buf[4];
132 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
135 lbx_errno = LBX_EEOF;
140 img->palstart = unpack_16_le(buf+0);
141 img->palcount = unpack_16_le(buf+2);
142 img->foff += sizeof buf;
143 img->paloff = img->foff;
145 if (img->palstart + img->palcount > 256) {
146 lbx_errno = LBX_EFORMAT;
155 static int _lbx_drawrow(int first, struct lbx_image *img)
157 unsigned short type, count, yval, xval;
158 unsigned char buf[4];
162 assert(img->framedata);
165 if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
168 type = unpack_16_le(buf+0);
177 yval = unpack_16_le(buf+2);
181 if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
184 count = unpack_16_le(buf+0);
186 xval = unpack_16_le(buf+2);
190 /* Ensure that the row fits in the image. */
191 if (img->height - img->currenty <= yval || xval >= img->width) {
192 lbx_errno = LBX_EFORMAT;
196 img->currenty += yval;
197 img->currentx = xval;
199 xval = unpack_16_le(buf+2);
201 if (img->width - img->currentx <= xval) {
202 lbx_errno = LBX_EFORMAT;
205 img->currentx += xval;
210 if (count > img->width - img->currentx) {
211 lbx_errno = LBX_EFORMAT;
215 memset(&img->mask[img->currenty][img->currentx], 1, count);
217 pos = &img->framedata[img->currenty][img->currentx];
218 rc = fread(pos, 1, count, img->f);
226 if (fread(buf, 1, 1, img->f) != 1)
234 lbx_errno = LBX_EEOF;
241 static unsigned char **allocframebuffer(size_t width, size_t height)
243 unsigned char **new, *tmp;
246 tmp = calloc(height, width);
252 new = malloc(height * sizeof *new);
259 for (i = 0; i < height; i++) {
260 new[i] = tmp + i * width;
266 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
268 if (frame >= img->frames || frame < 0) {
269 lbx_errno = LBX_ERANGE;
273 if (!img->framedata) {
274 img->framedata = allocframebuffer(img->width, img->height);
280 img->mask = allocframebuffer(img->width, img->height);
285 /* Start over if we are backtracking. */
286 if (img->currentframe > frame)
287 img->currentframe == -1;
289 if (img->flags & FLAG_OVERWRITE) {
290 /* Clear the slate. */
291 memset(img->framedata[0], 0, img->width * img->height);
292 memset(img->mask[0], 0, img->width * img->height);
294 /* We must have previous frame decoded to continue. */
295 if (frame > img->currentframe + 1) {
296 if (!lbximg_getframe(img, frame-1))
301 if (img->currentframe != frame) {
304 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
308 rc = _lbx_drawrow(first, img);
313 if (!rc && img->foff > img->offsets[frame+1]) {
314 lbx_errno = LBX_EFORMAT;
320 img->currentframe = frame;
321 return img->framedata;
324 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
329 for (i = 0; i < 256; i++) {
330 if (fread(entry, sizeof entry, 1, f) != 1) {
331 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
336 lbx_errno = LBX_EFORMAT;
340 palette[i] = (struct lbx_colour){
341 .red = entry[1] << 2,
342 .green = entry[2] << 2,
343 .blue = entry[3] << 2,
351 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
358 /* Do nothing if the image doesn't have embedded palette data. */
359 if (!(img->flags & FLAG_PALETTE))
362 if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
365 for (i = 0; i < img->palcount; i++) {
366 rc = fread(entry, 1, sizeof entry, img->f);
369 if (rc < sizeof entry) {
374 lbx_errno = LBX_EFORMAT;
378 palette[img->palstart + i] = (struct lbx_colour){
379 .red = entry[1] << 2,
380 .green = entry[2] << 2,
381 .blue = entry[3] << 2,
387 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
391 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
393 *info = (struct lbx_imginfo) {
395 .height = img->height,
396 .nframes = img->frames,
397 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
400 /* There seems to be two ways of specifying that an image loops. */
401 if (img->flags & FLAG_LOOPING) {
404 } else if (img->leadin != img->frames - 1) {
405 info->loopstart = img->leadin;
410 unsigned char **lbximg_getmask(struct lbx_image *img)
415 void lbximg_close(struct lbx_image *img)
419 if (img->framedata) {
420 free(img->framedata[0]);
421 free(img->framedata);