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