]> git.draconx.ca Git - liblbx.git/blob - src/image.c
liblbx: Use unconditional includes of <config.h>.
[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 (C) 2006-2010 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 <errno.h>
24
25 #include "pack.h"
26 #include "misc.h"
27 #include "lbx.h"
28 #include "error.h"
29 #include "image.h"
30
31 #define FLAG_RAW       0x0100 /* Image is stored as a flat array of bytes. */
32 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
33 #define FLAG_BUILDING  0x0800 /* Buildings have this, related to shadow? */
34 #define FLAG_PALETTE   0x1000 /* Image contains embedded palette. */
35 #define FLAG_LOOPING   0x2000 /* Loop over all frames in the image (unsure). */
36
37 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
38
39 #define HDR_LEN 12
40
41 struct lbx_image {
42         unsigned short width, height;
43         unsigned short wtf, flags;
44         unsigned char  frames, wtf2, leadin, chunk;
45         unsigned short palstart, palcount;
46
47         const struct lbx_file_ops *fops;
48         int (*dtor)(void *handle);
49         void *f;
50
51         long paloff;
52
53         int currentframe;
54         int currentx, currenty;
55         unsigned char **framedata;
56         unsigned char **mask;
57
58         unsigned long offsets[];
59 };
60
61 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
62 {
63         unsigned short nframes = unpack_16_le(hdr+6);
64         struct lbx_image *img;
65
66         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
67         if (!img) {
68                 lbx_error_raise(LBX_ENOMEM);
69                 return NULL;
70         }
71
72         *img = (struct lbx_image) {
73                 .width  = unpack_16_le(hdr+0),
74                 .height = unpack_16_le(hdr+2),
75                 .wtf    = unpack_16_le(hdr+4),
76                 .frames = hdr[6],
77                 .wtf2   = hdr[7],
78                 .leadin = hdr[8],
79                 .chunk  = hdr[9],
80                 .flags  = unpack_16_le(hdr+10),
81
82                 .currentframe = -1,
83         };
84
85         return img;
86 }
87
88 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
89                               int (*destructor)(void *))
90 {
91         unsigned char hdr_buf[HDR_LEN];
92         struct lbx_image *img;
93
94         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
95                 if (fops->eof(f))
96                         lbx_error_raise(LBX_EEOF);
97                 return NULL;
98         }
99
100         img = lbximg_init(hdr_buf);
101         if (!img)
102                 return NULL;
103
104         img->f    = f;
105         img->fops = fops;
106         img->dtor = destructor;
107
108         /*
109          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
110          * files which differ from what I believe to be true of all LBX images.
111          * When we can decode every image, then these assertions should be
112          * replaced with constraints.
113          */
114         _lbx_assert(img->wtf  == 0); /* version? */
115         _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
116         _lbx_assert(img->frames > img->leadin);
117         _lbx_assert(!(img->flags & ~FLAG_ALL));
118
119         /* Read all offsets.  Should be merged with identical code in lbx.c */
120         for (unsigned i = 0; i <= img->frames; i++) {
121                 unsigned char buf[4];
122
123                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
124                         if (fops->eof(f))
125                                 lbx_error_raise(LBX_EEOF);
126                         free(img);
127                         return NULL;
128                 }
129
130                 img->offsets[i] = unpack_32_le(buf);
131         }
132
133         if (img->flags & FLAG_PALETTE) {
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->palstart = unpack_16_le(buf+0);
144                 img->palcount = unpack_16_le(buf+2);
145                 img->paloff   = fops->tell(f);
146
147                 if (img->palstart + img->palcount > 256) {
148                         lbx_error_raise(LBX_EFORMAT);
149                         free(img);
150                         return NULL;
151                 }
152         }
153
154         return img;
155 }
156
157 static int pipe_close(void *f)
158 {
159         struct lbx_pipe_state *p = f;
160         int rc;
161
162         rc = fclose(p->f);
163         free(p);
164         return rc;
165 }
166
167 static int file_close(void *f)
168 {
169         return fclose((FILE *)f);
170 }
171
172 struct lbx_image *lbximg_fopen(const char *file)
173 {
174         struct lbx_pipe_state *p;
175         FILE *f;
176
177         f = fopen(file, "rb");
178         if (!f)
179                 return NULL;
180
181         if (fseek(f, 0, SEEK_CUR) == 0)
182                 return lbximg_open(f, &lbx_default_fops, file_close);
183
184         p = malloc(sizeof *p);
185         if (!p) {
186                 fclose(f);
187                 return NULL;
188         }
189
190         *p = (struct lbx_pipe_state) { .f = f };
191         return lbximg_open(p, &lbx_pipe_fops, pipe_close);
192 }
193
194 static int _lbx_drawrow(int first, struct lbx_image *img)
195 {
196         unsigned short type, count, yval, xval;
197         unsigned char buf[4];
198         unsigned char *pos;
199         size_t rc;
200
201         assert(img->framedata);
202         assert(img->mask);
203
204         if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
205                 goto readerr;
206         type = unpack_16_le(buf+0);
207
208         if (first) {
209                 img->currentx = 0;
210                 img->currenty = 0;
211                 type = 0;
212         }
213
214         if (type == 0) {
215                 yval = unpack_16_le(buf+2);
216                 if (yval == 1000)
217                         return 1;
218
219                 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
220                         goto readerr;
221                 count = unpack_16_le(buf+0);
222
223                 xval = unpack_16_le(buf+2);
224                 if (xval == 1000)
225                         return 1;
226
227                 /* Ensure that the row fits in the image. */
228                 if (img->height - img->currenty <= yval || xval >= img->width) {
229                         lbx_error_raise(LBX_EFORMAT);
230                         return -1;
231                 }
232
233                 img->currenty += yval;
234                 img->currentx  = xval;
235         } else {
236                 xval = unpack_16_le(buf+2);
237
238                 if (img->width - img->currentx <= xval) {
239                         lbx_error_raise(LBX_EFORMAT);
240                         return -1;
241                 }
242                 img->currentx += xval;
243
244                 count = type;
245         }
246
247         if (count > img->width - img->currentx) {
248                 lbx_error_raise(LBX_EFORMAT);
249                 return -1;
250         }
251
252         memset(&img->mask[img->currenty][img->currentx], 1, count);
253
254         pos = &img->framedata[img->currenty][img->currentx];
255         rc  = img->fops->read(pos, count, img->f);
256         img->currentx += rc;
257
258         if (rc < count)
259                 goto readerr;
260
261         if (count % 2) {
262                 if (img->fops->read(buf, 1, img->f) != 1)
263                         goto readerr;
264         }
265
266         return 0;
267 readerr:
268         if (img->fops->eof(img->f))
269                 lbx_error_raise(LBX_EEOF);
270         return -1;
271 }
272
273 static unsigned char **allocframebuffer(size_t width, size_t height)
274 {
275         unsigned char **new, *tmp;
276         size_t i;
277
278         tmp = calloc(height, width);
279         if (!tmp) {
280                 lbx_error_raise(LBX_ENOMEM);
281                 return NULL;
282         }
283
284         new = malloc(height * sizeof *new);
285         if (!new) {
286                 lbx_error_raise(LBX_ENOMEM);
287                 free(tmp);
288                 return NULL;
289         }
290
291         for (i = 0; i < height; i++) {
292                 new[i] = tmp + i * width;
293         }
294
295         return new;
296 }
297
298 static unsigned char **read_raw_frame(struct lbx_image *img, int frame)
299 {
300         unsigned long size = img->width * img->height;
301
302         assert(img->flags & FLAG_RAW);
303
304         if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
305                 return NULL;
306         }
307
308         if (img->fops->read(img->framedata[0], size, img->f) != size) {
309                 if (img->fops->eof(img->f))
310                         lbx_error_raise(LBX_EEOF);
311                 return NULL;
312         }
313         memset(img->mask[0], 1, size);
314
315         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
316                 lbx_error_raise(LBX_EFORMAT);
317                 return NULL;
318         }
319
320         return img->framedata;
321 }
322
323 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
324 {
325         if (frame >= img->frames || frame < 0) {
326                 lbx_error_raise(LBX_ENOENT);
327                 return NULL;
328         }
329
330         if (!img->framedata) {
331                 img->framedata = allocframebuffer(img->width, img->height);
332                 if (!img->framedata)
333                         return NULL;
334         }
335
336         if (!img->mask) {
337                 img->mask = allocframebuffer(img->width, img->height);
338                 if (!img->mask)
339                         return NULL;
340         }
341
342         if (img->flags & FLAG_RAW)
343                 return read_raw_frame(img, frame);
344
345         if ((img->flags & FLAG_OVERWRITE)
346              || (img->chunk && !(frame % img->chunk))) {
347                 /* Clear the slate. */
348                 img->currentframe = -1;
349                 memset(img->framedata[0], 0, img->width * img->height);
350                 memset(img->mask[0],      0, img->width * img->height);
351         } else {
352                 /* Start over if we are backtracking. */
353                 if (img->currentframe > frame) {
354                         memset(img->mask[0], 0, img->width * img->height);
355                         img->currentframe = -1;
356                 }
357
358                 /* We must have previous frame decoded to continue. */
359                 if (frame > img->currentframe + 1) {
360                         if (!lbximg_getframe(img, frame-1))
361                                 return NULL;
362                 }
363         }
364
365         if (img->currentframe != frame) {
366                 int rc, first = 1;
367
368                 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
369                         return NULL;
370                 }
371
372                 do {
373                         rc = _lbx_drawrow(first, img);
374                         if (rc == -1)
375                                 return NULL;
376                         first = 0;
377
378                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
379                                 lbx_error_raise(LBX_EFORMAT);
380                                 return NULL;
381                         }
382                 } while (!rc);
383         }
384
385         img->currentframe = frame;
386         return img->framedata;
387 }
388
389 int
390 lbximg_loadpalette(void *f, const struct lbx_file_ops *fops,
391                    struct lbx_colour palette[static 256])
392 {
393         unsigned char entry[4];
394         int i;
395
396         for (i = 0; i < 256; i++) {
397                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
398                         if (fops->eof(f))
399                                 lbx_error_raise(LBX_EEOF);
400                         return -1;
401                 }
402
403                 if (entry[0] != 1) {
404                         lbx_error_raise(LBX_EFORMAT);
405                         return -1;
406                 }
407
408                 palette[i] = (struct lbx_colour) {
409                         .red    = entry[1] << 2,
410                         .green  = entry[2] << 2,
411                         .blue   = entry[3] << 2,
412                         .active = 1,
413                 };
414         }
415
416         return 0;
417 }
418
419 int
420 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
421 {
422         unsigned char entry[4];
423         unsigned int i;
424         size_t rc;
425
426         /* Do nothing if the image doesn't have embedded palette data. */
427         if (!(img->flags & FLAG_PALETTE))
428                 return 0;
429
430         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
431                 return -1;
432         }
433
434         for (i = 0; i < img->palcount; i++) {
435                 rc = img->fops->read(entry, sizeof entry, img->f);
436                 if (rc < sizeof entry) {
437                         goto readerr;
438                 }
439
440                 if (entry[0] != 0) {
441                         lbx_error_raise(LBX_EFORMAT);
442                         return -1;
443                 }
444
445                 palette[img->palstart + i] = (struct lbx_colour){
446                         .red    = entry[1] << 2,
447                         .green  = entry[2] << 2,
448                         .blue   = entry[3] << 2,
449                         .active = 1,
450                 };
451         }
452
453         return 0;
454 readerr:
455         if (img->fops->eof(img->f))
456                 lbx_error_raise(LBX_EEOF);
457         return -1;
458 }
459
460 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
461 {
462         *info = (struct lbx_imginfo) {
463                 .width      = img->width,
464                 .height     = img->height,
465                 .nframes    = img->frames,
466                 .chunk      = img->chunk,
467                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
468         };
469
470         /* There seems to be two ways of specifying that an image loops. */
471         if (img->flags & FLAG_LOOPING) {
472                 info->loopstart = 0;
473                 info->looping   = 1;
474         } else if (img->leadin != img->frames - 1) {
475                 info->loopstart = img->leadin;
476                 info->looping   = 1;
477         }
478 }
479
480 unsigned char **lbximg_getmask(struct lbx_image *img)
481 {
482         return img->mask;
483 }
484
485 int lbximg_close(struct lbx_image *img)
486 {
487         int rc = 0;
488
489         if (img) {
490                 if (img->framedata) {
491                         free(img->framedata[0]);
492                         free(img->framedata);
493                 }
494
495                 if (img->mask) {
496                         free(img->mask[0]);
497                         free(img->mask);
498                 }
499
500                 if (img && img->dtor) {
501                         rc = img->dtor(img->f);
502                 }
503
504                 free(img);
505         }
506
507         return rc;
508 }