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 if (img->flags & FLAG_OVERWRITE) {
286 /* Clear the slate. */
287 img->currentframe = -1;
288 memset(img->framedata[0], 0, img->width * img->height);
289 memset(img->mask[0], 0, img->width * img->height);
291 /* Start over if we are backtracking. */
292 if (img->currentframe > frame)
293 img->currentframe = -1;
295 /* We must have previous frame decoded to continue. */
296 if (frame > img->currentframe + 1) {
297 if (!lbximg_getframe(img, frame-1))
302 if (img->currentframe != frame) {
305 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
311 rc = _lbx_drawrow(first, img);
316 if (img->fops->tell(img->f) > img->offsets[frame+1]) {
317 lbx_errno = LBX_EFORMAT;
323 img->currentframe = frame;
324 return img->framedata;
327 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
329 unsigned char entry[4];
332 for (i = 0; i < 256; i++) {
333 if (fread(entry, sizeof entry, 1, f) != 1) {
334 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
339 lbx_errno = LBX_EFORMAT;
343 palette[i] = (struct lbx_colour){
344 .red = entry[1] << 2,
345 .green = entry[2] << 2,
346 .blue = entry[3] << 2,
355 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
357 unsigned char entry[4];
361 /* Do nothing if the image doesn't have embedded palette data. */
362 if (!(img->flags & FLAG_PALETTE))
365 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
370 for (i = 0; i < img->palcount; i++) {
371 rc = img->fops->read(entry, sizeof entry, img->f);
372 if (rc < sizeof entry) {
377 lbx_errno = LBX_EFORMAT;
381 palette[img->palstart + i] = (struct lbx_colour){
382 .red = entry[1] << 2,
383 .green = entry[2] << 2,
384 .blue = entry[3] << 2,
391 lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
395 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
397 *info = (struct lbx_imginfo) {
399 .height = img->height,
400 .nframes = img->frames,
401 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
404 /* There seems to be two ways of specifying that an image loops. */
405 if (img->flags & FLAG_LOOPING) {
408 } else if (img->leadin != img->frames - 1) {
409 info->loopstart = img->leadin;
414 unsigned char **lbximg_getmask(struct lbx_image *img)
419 int lbximg_close(struct lbx_image *img)
424 if (img->framedata) {
425 free(img->framedata[0]);
426 free(img->framedata);
434 if (img && img->dtor) {
435 rc = img->dtor(img->f);