]> git.draconx.ca Git - liblbx.git/blob - src/image.c
liblbx: Update lbximg_fopen to work like lbx_fopen.
[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 char  frames, wtf2, leadin, chunk;
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 = hdr[6],
79                 .wtf2   = hdr[7],
80                 .leadin = hdr[8],
81                 .chunk  = hdr[9],
82                 .flags  = unpack_16_le(hdr+10),
83
84                 .currentframe = -1,
85         };
86
87         return img;
88 }
89
90 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
91                               int (*destructor)(void *))
92 {
93         unsigned char hdr_buf[HDR_LEN];
94         struct lbx_image *img;
95
96         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
97                 lbx_errno = -errno;
98                 if (fops->eof(f))
99                         lbx_errno = 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                         lbx_errno = -errno;
128                         if (fops->eof(f))
129                                 lbx_errno = LBX_EEOF;
130                         free(img);
131                         return NULL;
132                 }
133
134                 img->offsets[i] = unpack_32_le(buf);
135         }
136
137         if (img->flags & FLAG_PALETTE) {
138                 unsigned char buf[4];
139
140                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
141                         lbx_errno = -errno;
142                         if (fops->eof(f))
143                                 lbx_errno = LBX_EEOF;
144                         free(img);
145                         return NULL;
146                 }
147
148                 img->palstart = unpack_16_le(buf+0);
149                 img->palcount = unpack_16_le(buf+2);
150                 img->paloff   = fops->tell(f);
151
152                 if (img->palstart + img->palcount > 256) {
153                         lbx_errno = LBX_EFORMAT;
154                         free(img);
155                         return NULL;
156                 }
157         }
158
159         return img;
160 }
161
162 static int pipe_close(void *f)
163 {
164         struct lbx_pipe_state *p = f;
165         int rc;
166
167         rc = fclose(p->f);
168         free(p);
169         return rc;
170 }
171
172 static int file_close(void *f)
173 {
174         return fclose((FILE *)f);
175 }
176
177 struct lbx_image *lbximg_fopen(const char *file)
178 {
179         struct lbx_pipe_state *p;
180         FILE *f;
181
182         f = fopen(file, "rb");
183         if (!f)
184                 return NULL;
185
186         if (fseek(f, 0, SEEK_CUR) == 0)
187                 return lbximg_open(f, &lbx_default_fops, file_close);
188
189         p = malloc(sizeof *p);
190         if (!p) {
191                 fclose(f);
192                 return NULL;
193         }
194
195         *p = (struct lbx_pipe_state) { .f = f };
196         return lbximg_open(p, &lbx_pipe_fops, pipe_close);
197 }
198
199 static int _lbx_drawrow(int first, struct lbx_image *img)
200 {
201         unsigned short type, count, yval, xval;
202         unsigned char buf[4];
203         unsigned char *pos;
204         size_t rc;
205
206         assert(img->framedata);
207         assert(img->mask);
208
209         if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
210                 goto readerr;
211         type = unpack_16_le(buf+0);
212
213         if (first) {
214                 img->currentx = 0;
215                 img->currenty = 0;
216                 type = 0;
217         }
218
219         if (type == 0) {
220                 yval = unpack_16_le(buf+2);
221                 if (yval == 1000)
222                         return 1;
223
224                 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
225                         goto readerr;
226                 count = unpack_16_le(buf+0);
227
228                 xval = unpack_16_le(buf+2);
229                 if (xval == 1000)
230                         return 1;
231
232                 /* Ensure that the row fits in the image. */
233                 if (img->height - img->currenty <= yval || xval >= img->width) {
234                         lbx_errno = LBX_EFORMAT;
235                         return -1;
236                 }
237
238                 img->currenty += yval;
239                 img->currentx  = xval;
240         } else {
241                 xval = unpack_16_le(buf+2);
242
243                 if (img->width - img->currentx <= xval) {
244                         lbx_errno = LBX_EFORMAT;
245                         return -1;
246                 }
247                 img->currentx += xval;
248
249                 count = type;
250         }
251
252         if (count > img->width - img->currentx) {
253                 lbx_errno = LBX_EFORMAT;
254                 return -1;
255         }
256
257         memset(&img->mask[img->currenty][img->currentx], 1, count);
258
259         pos = &img->framedata[img->currenty][img->currentx];
260         rc  = img->fops->read(pos, count, img->f);
261         img->currentx += rc;
262
263         if (rc < count)
264                 goto readerr;
265
266         if (count % 2) {
267                 if (img->fops->read(buf, 1, img->f) != 1)
268                         goto readerr;
269         }
270
271         return 0;
272 readerr:
273         lbx_errno = -errno;
274         if (img->fops->eof(img->f))
275                 lbx_errno = LBX_EEOF;
276         return -1;
277 }
278
279 static unsigned char **allocframebuffer(size_t width, size_t height)
280 {
281         unsigned char **new, *tmp;
282         size_t i;
283
284         tmp = calloc(height, width);
285         if (!tmp) {
286                 lbx_errno = -errno;
287                 return NULL;
288         }
289
290         new = malloc(height * sizeof *new);
291         if (!new) {
292                 lbx_errno = -errno;
293                 free(tmp);
294                 return NULL;
295         }
296
297         for (i = 0; i < height; i++) {
298                 new[i] = tmp + i * width;
299         }
300
301         return new;
302 }
303
304 static unsigned char **read_raw_frame(struct lbx_image *img, int frame)
305 {
306         unsigned long size = img->width * img->height;
307
308         assert(img->flags & FLAG_RAW);
309
310         if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
311                 lbx_errno = -errno;
312                 return NULL;
313         }
314
315         if (img->fops->read(img->framedata[0], size, img->f) != size) {
316                 lbx_errno = -errno;
317                 if (img->fops->eof(img->f))
318                         lbx_errno = LBX_EEOF;
319                 return NULL;
320         }
321         memset(img->mask[0], 1, size);
322
323         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
324                 lbx_errno = LBX_EFORMAT;
325                 return NULL;
326         }
327
328         return img->framedata;
329 }
330
331 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
332 {
333         if (frame >= img->frames || frame < 0) {
334                 lbx_errno = LBX_ERANGE;
335                 return NULL;
336         }
337
338         if (!img->framedata) {
339                 img->framedata = allocframebuffer(img->width, img->height);
340                 if (!img->framedata)
341                         return NULL;
342         }
343
344         if (!img->mask) {
345                 img->mask = allocframebuffer(img->width, img->height);
346                 if (!img->mask)
347                         return NULL;
348         }
349
350         if (img->flags & FLAG_RAW)
351                 return read_raw_frame(img, frame);
352
353         if ((img->flags & FLAG_OVERWRITE)
354              || (img->chunk && !(frame % img->chunk))) {
355                 /* Clear the slate. */
356                 img->currentframe = -1;
357                 memset(img->framedata[0], 0, img->width * img->height);
358                 memset(img->mask[0],      0, img->width * img->height);
359         } else {
360                 /* Start over if we are backtracking. */
361                 if (img->currentframe > frame) {
362                         memset(img->mask[0], 0, img->width * img->height);
363                         img->currentframe = -1;
364                 }
365
366                 /* We must have previous frame decoded to continue. */
367                 if (frame > img->currentframe + 1) {
368                         if (!lbximg_getframe(img, frame-1))
369                                 return NULL;
370                 }
371         }
372
373         if (img->currentframe != frame) {
374                 int rc, first = 1;
375
376                 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
377                         lbx_errno = -errno;
378                         return NULL;
379                 }
380
381                 do {
382                         rc = _lbx_drawrow(first, img);
383                         if (rc == -1)
384                                 return NULL;
385                         first = 0;
386
387                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
388                                 lbx_errno = LBX_EFORMAT;
389                                 return NULL;
390                         }
391                 } while (!rc);
392         }
393
394         img->currentframe = frame;
395         return img->framedata;
396 }
397
398 int
399 lbximg_loadpalette(void *f, const struct lbx_file_ops *fops,
400                    struct lbx_colour palette[static 256])
401 {
402         unsigned char entry[4];
403         int i;
404
405         for (i = 0; i < 256; i++) {
406                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
407                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
408                         return -1;
409                 }
410
411                 if (entry[0] != 1) {
412                         lbx_errno = LBX_EFORMAT;
413                         return -1;
414                 }
415
416                 palette[i] = (struct lbx_colour) {
417                         .red    = entry[1] << 2,
418                         .green  = entry[2] << 2,
419                         .blue   = entry[3] << 2,
420                         .active = 1,
421                 };
422         }
423
424         return 0;
425 }
426
427 int
428 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
429 {
430         unsigned char entry[4];
431         unsigned int i;
432         size_t rc;
433
434         /* Do nothing if the image doesn't have embedded palette data. */
435         if (!(img->flags & FLAG_PALETTE))
436                 return 0;
437
438         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
439                 lbx_errno = -errno;
440                 return -1;
441         }
442
443         for (i = 0; i < img->palcount; i++) {
444                 rc = img->fops->read(entry, sizeof entry, img->f);
445                 if (rc < sizeof entry) {
446                         goto readerr;
447                 }
448
449                 if (entry[0] != 0) {
450                         lbx_errno = LBX_EFORMAT;
451                         return -1;
452                 }
453
454                 palette[img->palstart + i] = (struct lbx_colour){
455                         .red    = entry[1] << 2,
456                         .green  = entry[2] << 2,
457                         .blue   = entry[3] << 2,
458                         .active = 1,
459                 };
460         }
461
462         return 0;
463 readerr:
464         lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
465         return -1;
466 }
467
468 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
469 {
470         *info = (struct lbx_imginfo) {
471                 .width      = img->width,
472                 .height     = img->height,
473                 .nframes    = img->frames,
474                 .chunk      = img->chunk,
475                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
476         };
477
478         /* There seems to be two ways of specifying that an image loops. */
479         if (img->flags & FLAG_LOOPING) {
480                 info->loopstart = 0;
481                 info->looping   = 1;
482         } else if (img->leadin != img->frames - 1) {
483                 info->loopstart = img->leadin;
484                 info->looping   = 1;
485         }
486 }
487
488 unsigned char **lbximg_getmask(struct lbx_image *img)
489 {
490         return img->mask;
491 }
492
493 int lbximg_close(struct lbx_image *img)
494 {
495         int rc = 0;
496
497         if (img) {
498                 if (img->framedata) {
499                         free(img->framedata[0]);
500                         free(img->framedata);
501                 }
502
503                 if (img->mask) {
504                         free(img->mask[0]);
505                         free(img->mask);
506                 }
507
508                 if (img && img->dtor) {
509                         rc = img->dtor(img->f);
510                 }
511
512                 free(img);
513         }
514
515         return rc;
516 }