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/>.
33 #define FLAG_RAW 0x0100 /* Image is stored as a flat array of bytes. */
34 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
35 #define FLAG_BUILDING 0x0800 /* Buildings have this, related to shadow? */
36 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
37 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
39 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
43 /* States for image readout */
51 struct lbx_image_priv {
54 unsigned short wtf, flags;
57 const struct lbx_file_ops *fops;
58 int (*dtor)(void *handle);
61 /* State of frame readout */
62 unsigned currentx, currenty, currentn;
65 unsigned long offsets[FLEXIBLE_ARRAY_MEMBER];
68 static struct lbx_image_priv *lbx_img_init(unsigned char *hdr)
70 unsigned short nframes = unpack_16_le(hdr+6);
71 struct lbx_image_priv *img;
73 img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
75 lbx_error_raise(LBX_ENOMEM);
79 *img = (struct lbx_image_priv) {
80 .pub.width = unpack_16_le(hdr+0),
81 .pub.height = unpack_16_le(hdr+2),
82 .wtf = unpack_16_le(hdr+4),
87 .flags = unpack_16_le(hdr+10),
90 if (img->flags & FLAG_OVERWRITE)
93 if (img->flags & FLAG_LOOPING)
96 if (img->pub.leadin >= img->pub.frames) {
97 lbx_error_raise(LBX_EFORMAT);
105 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
106 int (*destructor)(void *))
108 unsigned char hdr_buf[HDR_LEN];
109 struct lbx_image_priv *img;
111 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
113 lbx_error_raise(LBX_EEOF);
117 img = lbx_img_init(hdr_buf);
123 img->dtor = destructor;
126 * DEBUG ONLY. These assertions exist to catch otherwise valid image
127 * files which differ from what I believe to be true of all LBX images.
128 * When we can decode every image, then these assertions should be
129 * replaced with constraints.
131 _lbx_assert(img->wtf == 0); /* version? */
132 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
133 _lbx_assert(!(img->flags & ~FLAG_ALL));
135 /* Read all offsets. Should be merged with identical code in lbx.c */
136 for (unsigned i = 0; i <= img->pub.frames; i++) {
137 unsigned char buf[4];
139 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
141 lbx_error_raise(LBX_EEOF);
146 img->offsets[i] = unpack_32_le(buf);
152 static int pipe_close(void *f)
154 struct lbx_pipe_state *p = f;
162 static int file_close(void *f)
164 return fclose((FILE *)f);
167 struct lbx_image *lbx_img_fopen(const char *file)
169 struct lbx_pipe_state *p;
172 f = fopen(file, "rb");
174 lbx_error_raise(-errno);
178 if (fseek(f, 0, SEEK_CUR) == 0)
179 return lbx_img_open(f, &lbx_default_fops, file_close);
181 p = malloc(sizeof *p);
183 lbx_error_raise(LBX_ENOMEM);
188 *p = (struct lbx_pipe_state) { .f = f };
189 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
192 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
194 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
196 if (frame >= pub->frames) {
197 lbx_error_raise(LBX_EINVAL);
201 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
205 if (!(img->flags & FLAG_RAW)) {
206 unsigned char buf[4];
208 /* Read frame header */
209 if (img->fops->read(buf, 4, img->f) != 4) {
210 if (img->fops->eof(img->f))
211 lbx_error_raise(LBX_EEOF);
215 if (unpack_16_le(buf) != 1) {
216 lbx_error_raise(LBX_EFORMAT);
221 img->currenty = unpack_16_le(buf+2);
222 if (img->currenty > img->pub.height) {
223 lbx_error_raise(LBX_EFORMAT);
228 img->read_state = READ_STATE_HEADER;
232 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
234 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
235 unsigned short length, offset;
236 unsigned char buf[4];
238 if (img->read_state != READ_STATE_HEADER) {
239 lbx_error_raise(LBX_EINVAL);
243 /* Raw images have no row headers */
244 if (img->flags & FLAG_RAW) {
245 img->currentn = img->pub.width;
246 *y = img->currenty++;
249 if (*y < img->pub.height) {
250 img->read_state = READ_STATE_DATA;
251 return img->currentn;
253 img->read_state = READ_STATE_DONE;
259 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
260 if (img->fops->eof(img->f))
261 lbx_error_raise(LBX_EEOF);
265 length = unpack_16_le(buf+0);
266 offset = unpack_16_le(buf+2);
269 if (offset == 1000) {
270 img->read_state = READ_STATE_DONE;
272 } else if (offset > img->pub.height - img->currenty) {
273 lbx_error_raise(LBX_EFORMAT);
277 img->currenty += offset;
280 } while (length == 0);
282 if (offset > img->pub.width - img->currentx) {
283 lbx_error_raise(LBX_EFORMAT);
286 img->currentx += offset;
288 if (length > img->pub.width - img->currentx) {
289 lbx_error_raise(LBX_EFORMAT);
292 img->currentn = length;
294 img->read_state = READ_STATE_DATA;
298 return img->currentn;
301 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
303 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
305 if (img->read_state != READ_STATE_DATA) {
306 lbx_error_raise(LBX_EINVAL);
310 if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
311 if (img->fops->eof(img->f))
312 lbx_error_raise(LBX_EEOF);
316 if (!(img->flags & FLAG_RAW) && img->currentn % 2) {
319 /* Skip padding byte */
320 if (img->fops->read(&c, 1, img->f) != 1) {
321 if (img->fops->eof(img->f))
322 lbx_error_raise(LBX_EEOF);
327 img->read_state = READ_STATE_HEADER;
328 img->currentx += img->currentn;
330 return img->currentn;
333 static int read_palette(void *f, const struct lbx_file_ops *fops,
334 struct lbx_colour *palette, unsigned count,
337 assert(count <= 256);
338 for (unsigned i = 0; i < count; i++) {
339 unsigned char buf[4];
341 if (fops->read(buf, 4, f) != 4) {
343 lbx_error_raise(LBX_EEOF);
347 if (buf[0] != external) {
348 lbx_error_raise(LBX_EFORMAT);
352 palette[i] = (struct lbx_colour) {
353 .red = buf[1] & 0x3f,
354 .green = buf[2] & 0x3f,
355 .blue = buf[3] & 0x3f,
363 int lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
364 struct lbx_colour *palette)
366 return read_palette(f, fops, palette, 256, true);
369 int lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour *out)
371 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
372 unsigned long palette_start, palette_count, palette_offset;
373 unsigned char buf[4];
376 /* Do nothing if the image doesn't have embedded palette data. */
377 if (!(img->flags & FLAG_PALETTE))
380 palette_offset = 16 + 4ul * img->pub.frames;
381 if (img->fops->seek(img->f, palette_offset, SEEK_SET)) {
385 /* Read embedded palette header */
386 if (img->fops->read(buf, 4, img->f) < 4)
389 palette_start = unpack_16_le(buf+0);
390 palette_count = unpack_16_le(buf+2);
391 if (palette_start + palette_count > 256) {
392 lbx_error_raise(LBX_EFORMAT);
397 rc = read_palette(img->f, img->fops,
398 out+palette_start, palette_count,
404 return palette_count;
406 if (img->fops->eof(img->f))
407 lbx_error_raise(LBX_EEOF);
411 int lbx_img_close(struct lbx_image *pub)
413 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
416 if (img && img->dtor) {
417 rc = img->dtor(img->f);