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