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