2 * 2ooM: The Master of Orion II Reverse Engineering Project
3 * Library for working with LBX image files.
4 * Copyright © 2006-2011, 2013-2014 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/>.
32 #define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */
33 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
34 #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */
35 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
36 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
38 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
42 /* States for image readout */
50 struct lbx_image_priv {
53 unsigned short wtf, flags;
55 unsigned short palstart, palcount;
57 const struct lbx_file_ops *fops;
58 int (*dtor)(void *handle);
63 /* State of frame readout */
64 unsigned currentx, currenty, currentn;
67 unsigned long offsets[];
70 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
72 unsigned short nframes = unpack_16_le(hdr+6);
73 struct lbx_image_priv *img;
75 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
77 lbx_error_raise(LBX_ENOMEM);
81 *img = (struct lbx_image_priv) {
82 .pub.width = unpack_16_le(hdr+0),
83 .pub.height = unpack_16_le(hdr+2),
84 .wtf = unpack_16_le(hdr+4),
89 .flags = unpack_16_le(hdr+10),
92 if (img->flags & FLAG_OVERWRITE)
95 if (img->flags & FLAG_LOOPING)
101 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
102 int (*destructor)(void *))
104 unsigned char hdr_buf[HDR_LEN];
105 struct lbx_image_priv *img;
107 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
109 lbx_error_raise(LBX_EEOF);
113 img = lbx_img_init(hdr_buf);
119 img->dtor = destructor;
122 * DEBUG ONLY. These assertions exist to catch otherwise valid image
123 * files which differ from what I believe to be true of all LBX images.
124 * When we can decode every image, then these assertions should be
125 * replaced with constraints.
127 _lbx_assert(img->wtf == 0); /* version? */
128 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
129 _lbx_assert(img->pub.frames > img->pub.leadin);
130 _lbx_assert(!(img->flags & ~FLAG_ALL));
132 /* Read all offsets. Should be merged with identical code in lbx.c */
133 for (unsigned i = 0; i <= img->pub.frames; i++) {
134 unsigned char buf[4];
136 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
138 lbx_error_raise(LBX_EEOF);
143 img->offsets[i] = unpack_32_le(buf);
146 if (img->flags & FLAG_PALETTE) {
147 unsigned char buf[4];
149 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
151 lbx_error_raise(LBX_EEOF);
156 img->palstart = unpack_16_le(buf+0);
157 img->palcount = unpack_16_le(buf+2);
158 img->paloff = fops->tell(f);
160 if (img->palstart + img->palcount > 256) {
161 lbx_error_raise(LBX_EFORMAT);
170 static int pipe_close(void *f)
172 struct lbx_pipe_state *p = f;
180 static int file_close(void *f)
182 return fclose((FILE *)f);
185 struct lbx_image *lbx_img_fopen(const char *file)
187 struct lbx_pipe_state *p;
190 f = fopen(file, "rb");
192 lbx_error_raise(-errno);
196 if (fseek(f, 0, SEEK_CUR) == 0)
197 return lbx_img_open(f, &lbx_default_fops, file_close);
199 p = malloc(sizeof *p);
201 lbx_error_raise(LBX_ENOMEM);
206 *p = (struct lbx_pipe_state) { .f = f };
207 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
210 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
212 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
214 if (frame >= pub->frames) {
215 lbx_error_raise(LBX_EINVAL);
219 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
223 if (!(img->flags & FLAG_RAW)) {
224 unsigned char buf[4];
226 /* Read frame header */
227 if (img->fops->read(buf, 4, img->f) != 4) {
228 if (img->fops->eof(img->f))
229 lbx_error_raise(LBX_EEOF);
233 if (unpack_16_le(buf) != 1) {
234 lbx_error_raise(LBX_EFORMAT);
239 img->currenty = unpack_16_le(buf+2);
240 if (img->currenty > img->pub.height) {
241 lbx_error_raise(LBX_EFORMAT);
246 img->read_state = READ_STATE_HEADER;
250 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
252 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
253 unsigned short length, offset;
254 unsigned char buf[4];
256 if (img->read_state != READ_STATE_HEADER) {
257 lbx_error_raise(LBX_EINVAL);
261 /* Raw images have no row headers */
262 if (img->flags & FLAG_RAW) {
263 img->currentn = img->pub.width;
264 *y = img->currenty++;
267 if (*y < img->pub.height) {
268 img->read_state = READ_STATE_DATA;
269 return img->currentn;
271 img->read_state = READ_STATE_DONE;
277 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
278 if (img->fops->eof(img->f))
279 lbx_error_raise(LBX_EEOF);
283 length = unpack_16_le(buf+0);
284 offset = unpack_16_le(buf+2);
287 if (offset == 1000) {
288 img->read_state = READ_STATE_DONE;
290 } else if (offset > img->pub.height - img->currenty) {
291 lbx_error_raise(LBX_EFORMAT);
295 img->currenty += offset;
298 } while (length == 0);
300 if (offset > img->pub.width - img->currentx) {
301 lbx_error_raise(LBX_EFORMAT);
304 img->currentx += offset;
306 if (length > img->pub.width - img->currentx) {
307 lbx_error_raise(LBX_EFORMAT);
310 img->currentn = length;
312 img->read_state = READ_STATE_DATA;
316 return img->currentn;
319 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
321 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
323 if (img->read_state != READ_STATE_DATA) {
324 lbx_error_raise(LBX_EINVAL);
328 if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
329 if (img->fops->eof(img->f))
330 lbx_error_raise(LBX_EEOF);
334 if (!(img->flags & FLAG_RAW)) {
335 /* Skip padding byte, if any */
336 if (img->currentn % 2) {
337 if (img->fops->seek(img->f, 1, SEEK_CUR))
342 img->read_state = READ_STATE_HEADER;
343 img->currentx += img->currentn;
345 return img->currentn;
349 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
350 struct lbx_colour palette[static 256])
352 unsigned char entry[4];
355 for (i = 0; i < 256; i++) {
356 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
358 lbx_error_raise(LBX_EEOF);
363 lbx_error_raise(LBX_EFORMAT);
367 palette[i] = (struct lbx_colour) {
368 .red = entry[1] & 0x3f,
369 .green = entry[2] & 0x3f,
370 .blue = entry[3] & 0x3f,
379 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
381 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
382 unsigned char entry[4];
386 /* Do nothing if the image doesn't have embedded palette data. */
387 if (!(img->flags & FLAG_PALETTE))
390 if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
394 for (i = 0; i < img->palcount; i++) {
395 rc = img->fops->read(entry, sizeof entry, img->f);
396 if (rc < sizeof entry) {
401 lbx_error_raise(LBX_EFORMAT);
405 palette[img->palstart + i] = (struct lbx_colour){
415 if (img->fops->eof(img->f))
416 lbx_error_raise(LBX_EEOF);
420 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
422 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
424 *info = (struct lbx_imginfo) {
425 .palettesz = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
428 /* There seems to be two ways of specifying that an image loops. */
429 if (img->flags & FLAG_LOOPING) {
432 } else if (img->pub.leadin != pub->frames - 1) {
433 info->loopstart = img->pub.leadin;
438 int lbx_img_close(struct lbx_image *pub)
440 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
443 if (img && img->dtor) {
444 rc = img->dtor(img->f);