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