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[];
68 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
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)
99 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
100 int (*destructor)(void *))
102 unsigned char hdr_buf[HDR_LEN];
103 struct lbx_image_priv *img;
105 if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
107 lbx_error_raise(LBX_EEOF);
111 img = lbx_img_init(hdr_buf);
117 img->dtor = destructor;
120 * DEBUG ONLY. These assertions exist to catch otherwise valid image
121 * files which differ from what I believe to be true of all LBX images.
122 * When we can decode every image, then these assertions should be
123 * replaced with constraints.
125 _lbx_assert(img->wtf == 0); /* version? */
126 _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
127 _lbx_assert(img->pub.frames > img->pub.leadin);
128 _lbx_assert(!(img->flags & ~FLAG_ALL));
130 /* Read all offsets. Should be merged with identical code in lbx.c */
131 for (unsigned i = 0; i <= img->pub.frames; i++) {
132 unsigned char buf[4];
134 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
136 lbx_error_raise(LBX_EEOF);
141 img->offsets[i] = unpack_32_le(buf);
147 static int pipe_close(void *f)
149 struct lbx_pipe_state *p = f;
157 static int file_close(void *f)
159 return fclose((FILE *)f);
162 struct lbx_image *lbx_img_fopen(const char *file)
164 struct lbx_pipe_state *p;
167 f = fopen(file, "rb");
169 lbx_error_raise(-errno);
173 if (fseek(f, 0, SEEK_CUR) == 0)
174 return lbx_img_open(f, &lbx_default_fops, file_close);
176 p = malloc(sizeof *p);
178 lbx_error_raise(LBX_ENOMEM);
183 *p = (struct lbx_pipe_state) { .f = f };
184 return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
187 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
189 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
191 if (frame >= pub->frames) {
192 lbx_error_raise(LBX_EINVAL);
196 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
200 if (!(img->flags & FLAG_RAW)) {
201 unsigned char buf[4];
203 /* Read frame header */
204 if (img->fops->read(buf, 4, img->f) != 4) {
205 if (img->fops->eof(img->f))
206 lbx_error_raise(LBX_EEOF);
210 if (unpack_16_le(buf) != 1) {
211 lbx_error_raise(LBX_EFORMAT);
216 img->currenty = unpack_16_le(buf+2);
217 if (img->currenty > img->pub.height) {
218 lbx_error_raise(LBX_EFORMAT);
223 img->read_state = READ_STATE_HEADER;
227 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
229 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
230 unsigned short length, offset;
231 unsigned char buf[4];
233 if (img->read_state != READ_STATE_HEADER) {
234 lbx_error_raise(LBX_EINVAL);
238 /* Raw images have no row headers */
239 if (img->flags & FLAG_RAW) {
240 img->currentn = img->pub.width;
241 *y = img->currenty++;
244 if (*y < img->pub.height) {
245 img->read_state = READ_STATE_DATA;
246 return img->currentn;
248 img->read_state = READ_STATE_DONE;
254 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
255 if (img->fops->eof(img->f))
256 lbx_error_raise(LBX_EEOF);
260 length = unpack_16_le(buf+0);
261 offset = unpack_16_le(buf+2);
264 if (offset == 1000) {
265 img->read_state = READ_STATE_DONE;
267 } else if (offset > img->pub.height - img->currenty) {
268 lbx_error_raise(LBX_EFORMAT);
272 img->currenty += offset;
275 } while (length == 0);
277 if (offset > img->pub.width - img->currentx) {
278 lbx_error_raise(LBX_EFORMAT);
281 img->currentx += offset;
283 if (length > img->pub.width - img->currentx) {
284 lbx_error_raise(LBX_EFORMAT);
287 img->currentn = length;
289 img->read_state = READ_STATE_DATA;
293 return img->currentn;
296 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
298 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
300 if (img->read_state != READ_STATE_DATA) {
301 lbx_error_raise(LBX_EINVAL);
305 if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
306 if (img->fops->eof(img->f))
307 lbx_error_raise(LBX_EEOF);
311 if (!(img->flags & FLAG_RAW)) {
312 /* Skip padding byte, if any */
313 if (img->currentn % 2) {
314 if (img->fops->seek(img->f, 1, SEEK_CUR))
319 img->read_state = READ_STATE_HEADER;
320 img->currentx += img->currentn;
322 return img->currentn;
325 static int read_palette(void *f, const struct lbx_file_ops *fops,
326 struct lbx_colour *palette, unsigned count,
329 assert(count <= 256);
330 for (unsigned i = 0; i < count; i++) {
331 unsigned char buf[4];
333 if (fops->read(buf, 4, f) != 4) {
335 lbx_error_raise(LBX_EEOF);
339 if (buf[0] != external) {
340 lbx_error_raise(LBX_EFORMAT);
344 palette[i] = (struct lbx_colour) {
345 .red = buf[1] & 0x3f,
346 .green = buf[2] & 0x3f,
347 .blue = buf[3] & 0x3f,
355 int lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
356 struct lbx_colour *palette)
358 return read_palette(f, fops, palette, 256, true);
361 int lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour *out)
363 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
364 unsigned long palette_start, palette_count, palette_offset;
365 unsigned char buf[4];
368 /* Do nothing if the image doesn't have embedded palette data. */
369 if (!(img->flags & FLAG_PALETTE))
372 palette_offset = 16 + 4ul * img->pub.frames;
373 if (img->fops->seek(img->f, palette_offset, SEEK_SET)) {
377 /* Read embedded palette header */
378 if (img->fops->read(buf, 4, img->f) < 4)
381 palette_start = unpack_16_le(buf+0);
382 palette_count = unpack_16_le(buf+2);
383 if (palette_start + palette_count > 256) {
384 lbx_error_raise(LBX_EFORMAT);
389 rc = read_palette(img->f, img->fops,
390 out+palette_start, palette_count,
396 return palette_count;
398 if (img->fops->eof(img->f))
399 lbx_error_raise(LBX_EEOF);
403 int lbx_img_close(struct lbx_image *pub)
405 struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
408 if (img && img->dtor) {
409 rc = img->dtor(img->f);