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/>.
33 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
34 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
35 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
36 #define FLAG_ALL (FLAG_OVERWRITE|FLAG_PALETTE|FLAG_LOOPING)
41 unsigned short width, height;
42 unsigned short wtf, flags;
43 unsigned short frames, leadin;
44 unsigned short palstart, palcount;
46 const struct lbx_file_ops *fops;
47 int (*dtor)(void *handle);
53 int currentx, currenty;
54 unsigned char **framedata;
57 unsigned long offsets[];
60 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
62 unsigned short nframes = unpack_16_le(hdr+6);
63 struct lbx_image *img;
65 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
71 *img = (struct lbx_image) {
72 .width = unpack_16_le(hdr+0),
73 .height = unpack_16_le(hdr+2),
74 .wtf = unpack_16_le(hdr+4),
75 .frames = unpack_16_le(hdr+6),
76 .leadin = unpack_16_le(hdr+8),
77 .flags = unpack_16_le(hdr+10),
85 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
86 int (*destructor)(void *))
88 unsigned char hdr_buf[HDR_LEN];
89 struct lbx_image *img;
91 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
98 img = lbximg_init(hdr_buf);
104 img->dtor = destructor;
107 * DEBUG ONLY. These assertions exist to catch otherwise valid image
108 * files which differ from what I believe to be true of all LBX images.
109 * If we never find any exceptions, we can replace the assertions with
112 _lbx_assert(img->wtf == 0); /* version? */
113 _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
114 _lbx_assert(!(img->flags & ~FLAG_ALL));
116 /* Read all offsets. Should be merged with identical code in lbx.c */
117 for (unsigned i = 0; i <= img->frames; i++) {
118 unsigned char buf[4];
120 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
123 lbx_errno = LBX_EEOF;
128 img->offsets[i] = unpack_32_le(buf);
131 if (img->flags & FLAG_PALETTE) {
132 unsigned char buf[4];
134 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
137 lbx_errno = LBX_EEOF;
142 img->palstart = unpack_16_le(buf+0);
143 img->palcount = unpack_16_le(buf+2);
144 img->paloff = fops->tell(f);
146 if (img->palstart + img->palcount > 256) {
147 lbx_errno = LBX_EFORMAT;
156 struct lbx_image *lbximg_fopen(FILE *f)
158 return lbximg_open(f, &lbx_default_fops, NULL);
161 static int _lbx_drawrow(int first, struct lbx_image *img)
163 unsigned short type, count, yval, xval;
164 unsigned char buf[4];
168 assert(img->framedata);
171 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
173 type = unpack_16_le(buf+0);
182 yval = unpack_16_le(buf+2);
186 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
188 count = unpack_16_le(buf+0);
190 xval = unpack_16_le(buf+2);
194 /* Ensure that the row fits in the image. */
195 if (img->height - img->currenty <= yval || xval >= img->width) {
196 lbx_errno = LBX_EFORMAT;
200 img->currenty += yval;
201 img->currentx = xval;
203 xval = unpack_16_le(buf+2);
205 if (img->width - img->currentx <= xval) {
206 lbx_errno = LBX_EFORMAT;
209 img->currentx += xval;
214 if (count > img->width - img->currentx) {
215 lbx_errno = LBX_EFORMAT;
219 memset(&img->mask[img->currenty][img->currentx], 1, count);
221 pos = &img->framedata[img->currenty][img->currentx];
222 rc = img->fops->read(pos, count, img->f);
229 if (img->fops->read(buf, 1, img->f) != 1)
236 if (img->fops->eof(img->f))
237 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 (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
310 rc = _lbx_drawrow(first, img);
315 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
316 lbx_errno = LBX_EFORMAT;
322 img->currentframe = frame;
323 return img->framedata;
326 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
328 unsigned char entry[4];
331 for (i = 0; i < 256; i++) {
332 if (fread(entry, sizeof entry, 1, f) != 1) {
333 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
338 lbx_errno = LBX_EFORMAT;
342 palette[i] = (struct lbx_colour){
343 .red = entry[1] << 2,
344 .green = entry[2] << 2,
345 .blue = entry[3] << 2,
353 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
355 unsigned char entry[4];
359 /* Do nothing if the image doesn't have embedded palette data. */
360 if (!(img->flags & FLAG_PALETTE))
363 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
368 for (i = 0; i < img->palcount; i++) {
369 rc = img->fops->read(entry, sizeof entry, img->f);
371 if (rc < sizeof entry) {
376 lbx_errno = LBX_EFORMAT;
380 palette[img->palstart + i] = (struct lbx_colour){
381 .red = entry[1] << 2,
382 .green = entry[2] << 2,
383 .blue = entry[3] << 2,
389 lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
393 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
395 *info = (struct lbx_imginfo) {
397 .height = img->height,
398 .nframes = img->frames,
399 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
402 /* There seems to be two ways of specifying that an image loops. */
403 if (img->flags & FLAG_LOOPING) {
406 } else if (img->leadin != img->frames - 1) {
407 info->loopstart = img->leadin;
412 unsigned char **lbximg_getmask(struct lbx_image *img)
417 int lbximg_close(struct lbx_image *img)
422 if (img->framedata) {
423 free(img->framedata[0]);
424 free(img->framedata);
432 if (img && img->dtor) {
433 rc = img->dtor(img->f);