]> git.draconx.ca Git - liblbx.git/blob - src/image.c
786e17aa729c43667b8f4b51be4303fbca85b2c1
[liblbx.git] / src / image.c
1 /*
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
5  *
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.
10  *
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.
15  *
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/>.
18  */
19 #include <config.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <assert.h>
23 #include <inttypes.h>
24 #include <errno.h>
25
26 #include "pack.h"
27 #include "misc.h"
28 #include "lbx.h"
29 #include "error.h"
30 #include "image.h"
31
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). */
37
38 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
39
40 #define HDR_LEN 12
41
42 /* States for image readout */
43 enum {
44         READ_STATE_INIT,
45         READ_STATE_HEADER,
46         READ_STATE_DATA,
47         READ_STATE_DONE,
48 };
49
50 struct lbx_image_priv {
51         struct lbx_image pub;
52
53         unsigned short wtf, flags;
54         unsigned char  wtf2;
55         unsigned short palstart, palcount;
56
57         const struct lbx_file_ops *fops;
58         int (*dtor)(void *handle);
59         void *f;
60
61         long paloff;
62
63         /* State of frame readout */
64         unsigned currentx, currenty, currentn;
65         int read_state;
66
67         unsigned long offsets[];
68 };
69
70 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
71 {
72         unsigned short nframes = unpack_16_le(hdr+6);
73         struct lbx_image_priv *img;
74
75         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
76         if (!img) {
77                 lbx_error_raise(LBX_ENOMEM);
78                 return NULL;
79         }
80
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),
85                 .pub.frames = hdr[6],
86                 .wtf2       = hdr[7],
87                 .pub.leadin = hdr[8],
88                 .pub.chunk  = hdr[9],
89                 .flags      = unpack_16_le(hdr+10),
90         };
91
92         if (img->flags & FLAG_OVERWRITE)
93                 img->pub.chunk = 1;
94
95         if (img->flags & FLAG_LOOPING)
96                 img->pub.leadin = 0;
97
98         return img;
99 }
100
101 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
102                                int (*destructor)(void *))
103 {
104         unsigned char hdr_buf[HDR_LEN];
105         struct lbx_image_priv *img;
106
107         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
108                 if (fops->eof(f))
109                         lbx_error_raise(LBX_EEOF);
110                 return NULL;
111         }
112
113         img = lbx_img_init(hdr_buf);
114         if (!img)
115                 return NULL;
116
117         img->f    = f;
118         img->fops = fops;
119         img->dtor = destructor;
120
121         /*
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.
126          */
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));
131
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];
135
136                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
137                         if (fops->eof(f))
138                                 lbx_error_raise(LBX_EEOF);
139                         free(img);
140                         return NULL;
141                 }
142
143                 img->offsets[i] = unpack_32_le(buf);
144         }
145
146         if (img->flags & FLAG_PALETTE) {
147                 unsigned char buf[4];
148
149                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
150                         if (fops->eof(f))
151                                 lbx_error_raise(LBX_EEOF);
152                         free(img);
153                         return NULL;
154                 }
155
156                 img->palstart = unpack_16_le(buf+0);
157                 img->palcount = unpack_16_le(buf+2);
158                 img->paloff   = fops->tell(f);
159
160                 if (img->palstart + img->palcount > 256) {
161                         lbx_error_raise(LBX_EFORMAT);
162                         free(img);
163                         return NULL;
164                 }
165         }
166
167         return &img->pub;
168 }
169
170 static int pipe_close(void *f)
171 {
172         struct lbx_pipe_state *p = f;
173         int rc;
174
175         rc = fclose(p->f);
176         free(p);
177         return rc;
178 }
179
180 static int file_close(void *f)
181 {
182         return fclose((FILE *)f);
183 }
184
185 struct lbx_image *lbx_img_fopen(const char *file)
186 {
187         struct lbx_pipe_state *p;
188         FILE *f;
189
190         f = fopen(file, "rb");
191         if (!f) {
192                 lbx_error_raise(-errno);
193                 return NULL;
194         }
195
196         if (fseek(f, 0, SEEK_CUR) == 0)
197                 return lbx_img_open(f, &lbx_default_fops, file_close);
198
199         p = malloc(sizeof *p);
200         if (!p) {
201                 lbx_error_raise(LBX_ENOMEM);
202                 fclose(f);
203                 return NULL;
204         }
205
206         *p = (struct lbx_pipe_state) { .f = f };
207         return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
208 }
209
210 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
211 {
212         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
213
214         if (frame >= pub->frames) {
215                 lbx_error_raise(LBX_EINVAL);
216                 return -1;
217         }
218
219         if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
220                 return -1;
221         }
222
223         if (!(img->flags & FLAG_RAW)) {
224                 unsigned char buf[4];
225
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);
230                         return -1;
231                 }
232
233                 if (unpack_16_le(buf) != 1) {
234                         lbx_error_raise(LBX_EFORMAT);
235                         return -1;
236                 }
237
238                 img->currentx = 0;
239                 img->currenty = unpack_16_le(buf+2);
240                 if (img->currenty > img->pub.height) {
241                         lbx_error_raise(LBX_EFORMAT);
242                         return -1;
243                 }
244         }
245
246         img->read_state = READ_STATE_HEADER;
247         return 0;
248 }
249
250 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
251 {
252         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
253         unsigned short length, offset;
254         unsigned char buf[4];
255
256         if (img->read_state != READ_STATE_HEADER) {
257                 lbx_error_raise(LBX_EINVAL);
258                 return -1;
259         }
260
261         /* Raw images have no row headers */
262         if (img->flags & FLAG_RAW) {
263                 img->currentn = img->pub.width;
264                 *y = img->currenty++;
265                 *x = 0;
266
267                 if (*y < img->pub.height) {
268                         img->read_state = READ_STATE_DATA;
269                         return img->currentn;
270                 } else {
271                         img->read_state = READ_STATE_DONE;
272                         return 0;
273                 }
274         }
275
276         do {
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);
280                         return -1;
281                 }
282
283                 length = unpack_16_le(buf+0);
284                 offset = unpack_16_le(buf+2);
285
286                 if (length == 0) {
287                         if (offset == 1000) {
288                                 img->read_state = READ_STATE_DONE;
289                                 return 0;
290                         } else if (offset > img->pub.height - img->currenty) {
291                                 lbx_error_raise(LBX_EFORMAT);
292                                 return -1;
293                         }
294
295                         img->currenty += offset;
296                         img->currentx  = 0;
297                 }
298         } while (length == 0);
299
300         if (offset > img->pub.width - img->currentx) {
301                 lbx_error_raise(LBX_EFORMAT);
302                 return -1;
303         }
304         img->currentx += offset;
305
306         if (length > img->pub.width - img->currentx) {
307                 lbx_error_raise(LBX_EFORMAT);
308                 return -1;
309         }
310         img->currentn = length;
311
312         img->read_state = READ_STATE_DATA;
313         *x = img->currentx;
314         *y = img->currenty;
315
316         return img->currentn;
317 }
318
319 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
320 {
321         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
322
323         if (img->read_state != READ_STATE_DATA) {
324                 lbx_error_raise(LBX_EINVAL);
325                 return -1;
326         }
327
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);
331                 return -1;
332         }
333
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))
338                                 return -1;
339                 }
340         }
341
342         img->read_state = READ_STATE_HEADER;
343         img->currentx += img->currentn;
344
345         return img->currentn;
346 }
347
348 int
349 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
350                     struct lbx_colour palette[static 256])
351 {
352         unsigned char entry[4];
353         int i;
354
355         for (i = 0; i < 256; i++) {
356                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
357                         if (fops->eof(f))
358                                 lbx_error_raise(LBX_EEOF);
359                         return -1;
360                 }
361
362                 if (entry[0] != 1) {
363                         lbx_error_raise(LBX_EFORMAT);
364                         return -1;
365                 }
366
367                 palette[i] = (struct lbx_colour) {
368                         .red    = entry[1] & 0x3f,
369                         .green  = entry[2] & 0x3f,
370                         .blue   = entry[3] & 0x3f,
371                         .active = 1,
372                 };
373         }
374
375         return 0;
376 }
377
378 int
379 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
380 {
381         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
382         unsigned char entry[4];
383         unsigned int i;
384         size_t rc;
385
386         /* Do nothing if the image doesn't have embedded palette data. */
387         if (!(img->flags & FLAG_PALETTE))
388                 return 0;
389
390         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
391                 return -1;
392         }
393
394         for (i = 0; i < img->palcount; i++) {
395                 rc = img->fops->read(entry, sizeof entry, img->f);
396                 if (rc < sizeof entry) {
397                         goto readerr;
398                 }
399
400                 if (entry[0] != 0) {
401                         lbx_error_raise(LBX_EFORMAT);
402                         return -1;
403                 }
404
405                 palette[img->palstart + i] = (struct lbx_colour){
406                         .red    = entry[1],
407                         .green  = entry[2],
408                         .blue   = entry[3],
409                         .active = 1,
410                 };
411         }
412
413         return 0;
414 readerr:
415         if (img->fops->eof(img->f))
416                 lbx_error_raise(LBX_EEOF);
417         return -1;
418 }
419
420 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
421 {
422         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
423
424         *info = (struct lbx_imginfo) {
425                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
426         };
427
428         /* There seems to be two ways of specifying that an image loops. */
429         if (img->flags & FLAG_LOOPING) {
430                 info->loopstart = 0;
431                 info->looping   = 1;
432         } else if (img->pub.leadin != pub->frames - 1) {
433                 info->loopstart = img->pub.leadin;
434                 info->looping   = 1;
435         }
436 }
437
438 int lbx_img_close(struct lbx_image *pub)
439 {
440         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
441         int rc = 0;
442
443         if (img && img->dtor) {
444                 rc = img->dtor(img->f);
445         }
446         free(img);
447
448         return rc;
449 }