]> git.draconx.ca Git - liblbx.git/blob - src/image.c
0caca9631aec54890711df84047de20dd93a6962
[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         int currentframe;
68         unsigned char **framedata;
69         unsigned char **mask;
70
71         unsigned long offsets[];
72 };
73
74 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
75 {
76         unsigned short nframes = unpack_16_le(hdr+6);
77         struct lbx_image_priv *img;
78
79         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
80         if (!img) {
81                 lbx_error_raise(LBX_ENOMEM);
82                 return NULL;
83         }
84
85         *img = (struct lbx_image_priv) {
86                 .pub.width  = unpack_16_le(hdr+0),
87                 .pub.height = unpack_16_le(hdr+2),
88                 .wtf        = unpack_16_le(hdr+4),
89                 .pub.frames = hdr[6],
90                 .wtf2       = hdr[7],
91                 .pub.leadin = hdr[8],
92                 .pub.chunk  = hdr[9],
93                 .flags      = unpack_16_le(hdr+10),
94
95                 .currentframe = -1,
96         };
97
98         if (img->flags & FLAG_OVERWRITE)
99                 img->pub.chunk = 1;
100
101         if (img->flags & FLAG_LOOPING)
102                 img->pub.leadin = 0;
103
104         return img;
105 }
106
107 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
108                                int (*destructor)(void *))
109 {
110         unsigned char hdr_buf[HDR_LEN];
111         struct lbx_image_priv *img;
112
113         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
114                 if (fops->eof(f))
115                         lbx_error_raise(LBX_EEOF);
116                 return NULL;
117         }
118
119         img = lbx_img_init(hdr_buf);
120         if (!img)
121                 return NULL;
122
123         img->f    = f;
124         img->fops = fops;
125         img->dtor = destructor;
126
127         /*
128          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
129          * files which differ from what I believe to be true of all LBX images.
130          * When we can decode every image, then these assertions should be
131          * replaced with constraints.
132          */
133         _lbx_assert(img->wtf  == 0); /* version? */
134         _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
135         _lbx_assert(img->pub.frames > img->pub.leadin);
136         _lbx_assert(!(img->flags & ~FLAG_ALL));
137
138         /* Read all offsets.  Should be merged with identical code in lbx.c */
139         for (unsigned i = 0; i <= img->pub.frames; i++) {
140                 unsigned char buf[4];
141
142                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
143                         if (fops->eof(f))
144                                 lbx_error_raise(LBX_EEOF);
145                         free(img);
146                         return NULL;
147                 }
148
149                 img->offsets[i] = unpack_32_le(buf);
150         }
151
152         if (img->flags & FLAG_PALETTE) {
153                 unsigned char buf[4];
154
155                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
156                         if (fops->eof(f))
157                                 lbx_error_raise(LBX_EEOF);
158                         free(img);
159                         return NULL;
160                 }
161
162                 img->palstart = unpack_16_le(buf+0);
163                 img->palcount = unpack_16_le(buf+2);
164                 img->paloff   = fops->tell(f);
165
166                 if (img->palstart + img->palcount > 256) {
167                         lbx_error_raise(LBX_EFORMAT);
168                         free(img);
169                         return NULL;
170                 }
171         }
172
173         return &img->pub;
174 }
175
176 static int pipe_close(void *f)
177 {
178         struct lbx_pipe_state *p = f;
179         int rc;
180
181         rc = fclose(p->f);
182         free(p);
183         return rc;
184 }
185
186 static int file_close(void *f)
187 {
188         return fclose((FILE *)f);
189 }
190
191 struct lbx_image *lbx_img_fopen(const char *file)
192 {
193         struct lbx_pipe_state *p;
194         FILE *f;
195
196         f = fopen(file, "rb");
197         if (!f) {
198                 lbx_error_raise(-errno);
199                 return NULL;
200         }
201
202         if (fseek(f, 0, SEEK_CUR) == 0)
203                 return lbx_img_open(f, &lbx_default_fops, file_close);
204
205         p = malloc(sizeof *p);
206         if (!p) {
207                 lbx_error_raise(LBX_ENOMEM);
208                 fclose(f);
209                 return NULL;
210         }
211
212         *p = (struct lbx_pipe_state) { .f = f };
213         return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
214 }
215
216 static int _lbx_drawrow(struct lbx_image_priv *img)
217 {
218         unsigned char *pos;
219         unsigned x, y;
220         long rc;
221
222         assert(img->framedata);
223         assert(img->mask);
224
225         rc = lbx_img_read_row_header(&img->pub, &x, &y);
226         if (rc == 0)
227                 return 1;
228         else if (rc < 0)
229                 return -1;
230
231         pos = &img->framedata[y][x];
232         rc = lbx_img_read_row_data(&img->pub, pos);
233         if (rc < 0)
234                 return -1;
235         memset(&img->mask[y][x], 1, rc);
236
237         return 0;
238 }
239
240 static unsigned char **allocframebuffer(size_t width, size_t height)
241 {
242         unsigned char **new, *tmp;
243         size_t i;
244
245         if (height > SIZE_MAX / sizeof *new) {
246                 lbx_error_raise(LBX_ENOMEM);
247                 return NULL;
248         }
249
250         /* Ensure that there is at least one row in the framebuffer. */
251         if (height == 0 || width == 0)
252                 width = height = 1;
253
254         tmp = calloc(height, width);
255         if (!tmp) {
256                 lbx_error_raise(LBX_ENOMEM);
257                 return NULL;
258         }
259
260         new = malloc(height * sizeof *new);
261         if (!new) {
262                 lbx_error_raise(LBX_ENOMEM);
263                 free(tmp);
264                 return NULL;
265         }
266
267         for (i = 0; i < height; i++) {
268                 new[i] = tmp + i * width;
269         }
270
271         return new;
272 }
273
274 int lbx_img_seek(struct lbx_image *pub, unsigned frame)
275 {
276         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
277
278         if (frame >= pub->frames) {
279                 lbx_error_raise(LBX_EINVAL);
280                 return -1;
281         }
282
283         if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
284                 return -1;
285         }
286
287         if (!(img->flags & FLAG_RAW)) {
288                 unsigned char buf[4];
289
290                 /* Read frame header */
291                 if (img->fops->read(buf, 4, img->f) != 4) {
292                         if (img->fops->eof(img->f))
293                                 lbx_error_raise(LBX_EEOF);
294                         return -1;
295                 }
296
297                 if (unpack_16_le(buf) != 1) {
298                         lbx_error_raise(LBX_EFORMAT);
299                         return -1;
300                 }
301
302                 img->currentx = 0;
303                 img->currenty = unpack_16_le(buf+2);
304                 if (img->currenty > img->pub.height) {
305                         lbx_error_raise(LBX_EFORMAT);
306                         return -1;
307                 }
308         }
309
310         img->read_state = READ_STATE_HEADER;
311         return 0;
312 }
313
314 long lbx_img_read_row_header(struct lbx_image *pub, unsigned *x, unsigned *y)
315 {
316         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
317         unsigned short length, offset;
318         unsigned char buf[4];
319
320         if (img->read_state != READ_STATE_HEADER) {
321                 lbx_error_raise(LBX_EINVAL);
322                 return -1;
323         }
324
325         /* Raw images have no row headers */
326         if (img->flags & FLAG_RAW) {
327                 img->currentn = img->pub.width;
328                 *y = img->currenty++;
329                 *x = 0;
330
331                 if (*y < img->pub.height) {
332                         img->read_state = READ_STATE_DATA;
333                         return img->currentn;
334                 } else {
335                         img->read_state = READ_STATE_DONE;
336                         return 0;
337                 }
338         }
339
340         do {
341                 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf) {
342                         if (img->fops->eof(img->f))
343                                 lbx_error_raise(LBX_EEOF);
344                         return -1;
345                 }
346
347                 length = unpack_16_le(buf+0);
348                 offset = unpack_16_le(buf+2);
349
350                 if (length == 0) {
351                         if (offset == 1000) {
352                                 img->read_state = READ_STATE_DONE;
353                                 return 0;
354                         } else if (offset > img->pub.height - img->currenty) {
355                                 lbx_error_raise(LBX_EFORMAT);
356                                 return -1;
357                         }
358
359                         img->currenty += offset;
360                         img->currentx  = 0;
361                 }
362         } while (length == 0);
363
364         if (offset > img->pub.width - img->currentx) {
365                 lbx_error_raise(LBX_EFORMAT);
366                 return -1;
367         }
368         img->currentx += offset;
369
370         if (length > img->pub.width - img->currentx) {
371                 lbx_error_raise(LBX_EFORMAT);
372                 return -1;
373         }
374         img->currentn = length;
375
376         img->read_state = READ_STATE_DATA;
377         *x = img->currentx;
378         *y = img->currenty;
379
380         return img->currentn;
381 }
382
383 long lbx_img_read_row_data(struct lbx_image *pub, void *buf)
384 {
385         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
386
387         if (img->read_state != READ_STATE_DATA) {
388                 lbx_error_raise(LBX_EINVAL);
389                 return -1;
390         }
391
392         if (img->fops->read(buf, img->currentn, img->f) != img->currentn) {
393                 if (img->fops->eof(img->f))
394                         lbx_error_raise(LBX_EEOF);
395                 return -1;
396         }
397
398         if (!(img->flags & FLAG_RAW)) {
399                 /* Skip padding byte, if any */
400                 if (img->currentn % 2) {
401                         if (img->fops->seek(img->f, 1, SEEK_CUR))
402                                 return -1;
403                 }
404         }
405
406         img->read_state = READ_STATE_HEADER;
407         img->currentx += img->currentn;
408
409         return img->currentn;
410 }
411
412 static unsigned char **read_raw_frame(struct lbx_image_priv *img, int frame)
413 {
414         unsigned long size = img->pub.width * img->pub.height;
415         int rc;
416
417         assert(img->flags & FLAG_RAW);
418
419         rc = lbx_img_seek(&img->pub, frame);
420         if (rc < 0)
421                 return NULL;
422
423         if (img->fops->read(img->framedata[0], size, img->f) != size) {
424                 if (img->fops->eof(img->f))
425                         lbx_error_raise(LBX_EEOF);
426                 return NULL;
427         }
428         memset(img->mask[0], 1, size);
429
430         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
431                 lbx_error_raise(LBX_EFORMAT);
432                 return NULL;
433         }
434
435         return img->framedata;
436 }
437
438 unsigned char **lbx_img_getframe(struct lbx_image *pub, int frame)
439 {
440         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
441         unsigned char buf[4];
442
443         if (frame >= pub->frames || frame < 0) {
444                 lbx_error_raise(LBX_EINVAL);
445                 return NULL;
446         }
447
448         if (!img->framedata) {
449                 img->framedata = allocframebuffer(pub->width, pub->height);
450                 if (!img->framedata)
451                         return NULL;
452         }
453
454         if (!img->mask) {
455                 img->mask = allocframebuffer(pub->width, pub->height);
456                 if (!img->mask)
457                         return NULL;
458         }
459
460         if (img->flags & FLAG_RAW)
461                 return read_raw_frame(img, frame);
462
463         if ((img->flags & FLAG_OVERWRITE)
464              || (pub->chunk && !(frame % pub->chunk))) {
465                 /* Clear the slate. */
466                 img->currentframe = -1;
467                 memset(img->framedata[0], 0, pub->width * pub->height);
468                 memset(img->mask[0],      0, pub->width * pub->height);
469         } else {
470                 /* Start over if we are backtracking. */
471                 if (img->currentframe > frame) {
472                         memset(img->mask[0], 0, pub->width * pub->height);
473                         img->currentframe = -1;
474                 }
475
476                 /* We must have previous frame decoded to continue. */
477                 if (frame > img->currentframe + 1) {
478                         if (!lbx_img_getframe(pub, frame-1))
479                                 return NULL;
480                 }
481         }
482
483         if (img->currentframe != frame) {
484                 int rc;
485
486                 rc = lbx_img_seek(pub, frame);
487                 if (rc < 0)
488                         return NULL;
489
490                 do {
491                         rc = _lbx_drawrow(img);
492                         if (rc == -1)
493                                 return NULL;
494
495                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
496                                 lbx_error_raise(LBX_EFORMAT);
497                                 return NULL;
498                         }
499                 } while (!rc);
500         }
501
502         img->currentframe = frame;
503         return img->framedata;
504 }
505
506 int
507 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
508                     struct lbx_colour palette[static 256])
509 {
510         unsigned char entry[4];
511         int i;
512
513         for (i = 0; i < 256; i++) {
514                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
515                         if (fops->eof(f))
516                                 lbx_error_raise(LBX_EEOF);
517                         return -1;
518                 }
519
520                 if (entry[0] != 1) {
521                         lbx_error_raise(LBX_EFORMAT);
522                         return -1;
523                 }
524
525                 palette[i] = (struct lbx_colour) {
526                         .red    = entry[1] & 0x3f,
527                         .green  = entry[2] & 0x3f,
528                         .blue   = entry[3] & 0x3f,
529                         .active = 1,
530                 };
531         }
532
533         return 0;
534 }
535
536 int
537 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
538 {
539         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
540         unsigned char entry[4];
541         unsigned int i;
542         size_t rc;
543
544         /* Do nothing if the image doesn't have embedded palette data. */
545         if (!(img->flags & FLAG_PALETTE))
546                 return 0;
547
548         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
549                 return -1;
550         }
551
552         for (i = 0; i < img->palcount; i++) {
553                 rc = img->fops->read(entry, sizeof entry, img->f);
554                 if (rc < sizeof entry) {
555                         goto readerr;
556                 }
557
558                 if (entry[0] != 0) {
559                         lbx_error_raise(LBX_EFORMAT);
560                         return -1;
561                 }
562
563                 palette[img->palstart + i] = (struct lbx_colour){
564                         .red    = entry[1],
565                         .green  = entry[2],
566                         .blue   = entry[3],
567                         .active = 1,
568                 };
569         }
570
571         return 0;
572 readerr:
573         if (img->fops->eof(img->f))
574                 lbx_error_raise(LBX_EEOF);
575         return -1;
576 }
577
578 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
579 {
580         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
581
582         *info = (struct lbx_imginfo) {
583                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
584         };
585
586         /* There seems to be two ways of specifying that an image loops. */
587         if (img->flags & FLAG_LOOPING) {
588                 info->loopstart = 0;
589                 info->looping   = 1;
590         } else if (img->pub.leadin != pub->frames - 1) {
591                 info->loopstart = img->pub.leadin;
592                 info->looping   = 1;
593         }
594 }
595
596 unsigned char **lbx_img_getmask(struct lbx_image *pub)
597 {
598         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
599
600         return img->mask;
601 }
602
603 int lbx_img_close(struct lbx_image *pub)
604 {
605         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
606         int rc = 0;
607
608         if (img) {
609                 if (img->framedata) {
610                         free(img->framedata[0]);
611                         free(img->framedata);
612                 }
613
614                 if (img->mask) {
615                         free(img->mask[0]);
616                         free(img->mask);
617                 }
618
619                 if (img && img->dtor) {
620                         rc = img->dtor(img->f);
621                 }
622
623                 free(img);
624         }
625
626         return rc;
627 }