]> git.draconx.ca Git - liblbx.git/blob - src/image.c
4657208a05ae28439496346473c8de7cb1566023
[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 <errno.h>
24
25 #include "pack.h"
26 #include "misc.h"
27 #include "lbx.h"
28 #include "error.h"
29 #include "image.h"
30
31 #define FLAG_RAW       0x0100 /* Image is stored as a flat array of bytes. */
32 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
33 #define FLAG_BUILDING  0x0800 /* Buildings have this, related to shadow? */
34 #define FLAG_PALETTE   0x1000 /* Image contains embedded palette. */
35 #define FLAG_LOOPING   0x2000 /* Loop over all frames in the image (unsure). */
36
37 #define FLAG_ALL (FLAG_RAW|FLAG_OVERWRITE|FLAG_BUILDING|FLAG_PALETTE|FLAG_LOOPING)
38
39 #define HDR_LEN 12
40
41 struct lbx_image_priv {
42         struct lbx_image pub;
43
44         unsigned short wtf, flags;
45         unsigned char  wtf2, leadin;
46         unsigned short palstart, palcount;
47
48         const struct lbx_file_ops *fops;
49         int (*dtor)(void *handle);
50         void *f;
51
52         long paloff;
53
54         int currentframe;
55         int currentx, currenty;
56         unsigned char **framedata;
57         unsigned char **mask;
58
59         unsigned long offsets[];
60 };
61
62 static struct lbx_image_priv *lbx_img_init(unsigned char hdr[static HDR_LEN])
63 {
64         unsigned short nframes = unpack_16_le(hdr+6);
65         struct lbx_image_priv *img;
66
67         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
68         if (!img) {
69                 lbx_error_raise(LBX_ENOMEM);
70                 return NULL;
71         }
72
73         *img = (struct lbx_image_priv) {
74                 .pub.width  = unpack_16_le(hdr+0),
75                 .pub.height = unpack_16_le(hdr+2),
76                 .wtf        = unpack_16_le(hdr+4),
77                 .pub.frames = hdr[6],
78                 .wtf2       = hdr[7],
79                 .leadin     = hdr[8],
80                 .pub.chunk  = hdr[9],
81                 .flags      = unpack_16_le(hdr+10),
82
83                 .currentframe = -1,
84         };
85
86         return img;
87 }
88
89 struct lbx_image *lbx_img_open(void *f, const struct lbx_file_ops *fops,
90                                int (*destructor)(void *))
91 {
92         unsigned char hdr_buf[HDR_LEN];
93         struct lbx_image_priv *img;
94
95         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
96                 if (fops->eof(f))
97                         lbx_error_raise(LBX_EEOF);
98                 return NULL;
99         }
100
101         img = lbx_img_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          * When we can decode every image, then these assertions should be
113          * replaced with constraints.
114          */
115         _lbx_assert(img->wtf  == 0); /* version? */
116         _lbx_assert(img->wtf2 == 0); /* very likely is simply reserved. */
117         _lbx_assert(img->pub.frames > img->leadin);
118         _lbx_assert(!(img->flags & ~FLAG_ALL));
119
120         /* Read all offsets.  Should be merged with identical code in lbx.c */
121         for (unsigned i = 0; i <= img->pub.frames; i++) {
122                 unsigned char buf[4];
123
124                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
125                         if (fops->eof(f))
126                                 lbx_error_raise(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                         if (fops->eof(f))
139                                 lbx_error_raise(LBX_EEOF);
140                         free(img);
141                         return NULL;
142                 }
143
144                 img->palstart = unpack_16_le(buf+0);
145                 img->palcount = unpack_16_le(buf+2);
146                 img->paloff   = fops->tell(f);
147
148                 if (img->palstart + img->palcount > 256) {
149                         lbx_error_raise(LBX_EFORMAT);
150                         free(img);
151                         return NULL;
152                 }
153         }
154
155         return &img->pub;
156 }
157
158 static int pipe_close(void *f)
159 {
160         struct lbx_pipe_state *p = f;
161         int rc;
162
163         rc = fclose(p->f);
164         free(p);
165         return rc;
166 }
167
168 static int file_close(void *f)
169 {
170         return fclose((FILE *)f);
171 }
172
173 struct lbx_image *lbx_img_fopen(const char *file)
174 {
175         struct lbx_pipe_state *p;
176         FILE *f;
177
178         f = fopen(file, "rb");
179         if (!f) {
180                 lbx_error_raise(-errno);
181                 return NULL;
182         }
183
184         if (fseek(f, 0, SEEK_CUR) == 0)
185                 return lbx_img_open(f, &lbx_default_fops, file_close);
186
187         p = malloc(sizeof *p);
188         if (!p) {
189                 lbx_error_raise(LBX_ENOMEM);
190                 fclose(f);
191                 return NULL;
192         }
193
194         *p = (struct lbx_pipe_state) { .f = f };
195         return lbx_img_open(p, &lbx_pipe_fops, pipe_close);
196 }
197
198 static int _lbx_drawrow(int first, struct lbx_image_priv *img)
199 {
200         unsigned short type, count, yval, xval;
201         unsigned char buf[4];
202         unsigned char *pos;
203         size_t rc;
204
205         assert(img->framedata);
206         assert(img->mask);
207
208         if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
209                 goto readerr;
210         type = unpack_16_le(buf+0);
211
212         if (first) {
213                 img->currentx = 0;
214                 img->currenty = 0;
215                 type = 0;
216         }
217
218         if (type == 0) {
219                 yval = unpack_16_le(buf+2);
220                 if (yval == 1000)
221                         return 1;
222
223                 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
224                         goto readerr;
225                 count = unpack_16_le(buf+0);
226
227                 xval = unpack_16_le(buf+2);
228                 if (xval == 1000)
229                         return 1;
230
231                 /* Ensure that the row fits in the image. */
232                 if (img->pub.height - img->currenty <= yval
233                     || xval >= img->pub.width) {
234                         lbx_error_raise(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->pub.width - img->currentx <= xval) {
244                         lbx_error_raise(LBX_EFORMAT);
245                         return -1;
246                 }
247                 img->currentx += xval;
248
249                 count = type;
250         }
251
252         if (count > img->pub.width - img->currentx) {
253                 lbx_error_raise(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         if (img->fops->eof(img->f))
274                 lbx_error_raise(LBX_EEOF);
275         return -1;
276 }
277
278 static unsigned char **allocframebuffer(size_t width, size_t height)
279 {
280         unsigned char **new, *tmp;
281         size_t i;
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
332         if (frame >= pub->frames || frame < 0) {
333                 lbx_error_raise(LBX_ENOENT);
334                 return NULL;
335         }
336
337         if (!img->framedata) {
338                 img->framedata = allocframebuffer(pub->width, pub->height);
339                 if (!img->framedata)
340                         return NULL;
341         }
342
343         if (!img->mask) {
344                 img->mask = allocframebuffer(pub->width, pub->height);
345                 if (!img->mask)
346                         return NULL;
347         }
348
349         if (img->flags & FLAG_RAW)
350                 return read_raw_frame(img, frame);
351
352         if ((img->flags & FLAG_OVERWRITE)
353              || (pub->chunk && !(frame % pub->chunk))) {
354                 /* Clear the slate. */
355                 img->currentframe = -1;
356                 memset(img->framedata[0], 0, pub->width * pub->height);
357                 memset(img->mask[0],      0, pub->width * pub->height);
358         } else {
359                 /* Start over if we are backtracking. */
360                 if (img->currentframe > frame) {
361                         memset(img->mask[0], 0, pub->width * pub->height);
362                         img->currentframe = -1;
363                 }
364
365                 /* We must have previous frame decoded to continue. */
366                 if (frame > img->currentframe + 1) {
367                         if (!lbx_img_getframe(pub, frame-1))
368                                 return NULL;
369                 }
370         }
371
372         if (img->currentframe != frame) {
373                 int rc, first = 1;
374
375                 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
376                         return NULL;
377                 }
378
379                 do {
380                         rc = _lbx_drawrow(first, img);
381                         if (rc == -1)
382                                 return NULL;
383                         first = 0;
384
385                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
386                                 lbx_error_raise(LBX_EFORMAT);
387                                 return NULL;
388                         }
389                 } while (!rc);
390         }
391
392         img->currentframe = frame;
393         return img->framedata;
394 }
395
396 int
397 lbx_img_loadpalette(void *f, const struct lbx_file_ops *fops,
398                     struct lbx_colour palette[static 256])
399 {
400         unsigned char entry[4];
401         int i;
402
403         for (i = 0; i < 256; i++) {
404                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
405                         if (fops->eof(f))
406                                 lbx_error_raise(LBX_EEOF);
407                         return -1;
408                 }
409
410                 if (entry[0] != 1) {
411                         lbx_error_raise(LBX_EFORMAT);
412                         return -1;
413                 }
414
415                 palette[i] = (struct lbx_colour) {
416                         .red    = entry[1] << 2,
417                         .green  = entry[2] << 2,
418                         .blue   = entry[3] << 2,
419                         .active = 1,
420                 };
421         }
422
423         return 0;
424 }
425
426 int
427 lbx_img_getpalette(struct lbx_image *pub, struct lbx_colour palette[static 256])
428 {
429         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
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                 return -1;
440         }
441
442         for (i = 0; i < img->palcount; i++) {
443                 rc = img->fops->read(entry, sizeof entry, img->f);
444                 if (rc < sizeof entry) {
445                         goto readerr;
446                 }
447
448                 if (entry[0] != 0) {
449                         lbx_error_raise(LBX_EFORMAT);
450                         return -1;
451                 }
452
453                 palette[img->palstart + i] = (struct lbx_colour){
454                         .red    = entry[1] << 2,
455                         .green  = entry[2] << 2,
456                         .blue   = entry[3] << 2,
457                         .active = 1,
458                 };
459         }
460
461         return 0;
462 readerr:
463         if (img->fops->eof(img->f))
464                 lbx_error_raise(LBX_EEOF);
465         return -1;
466 }
467
468 void lbx_img_getinfo(struct lbx_image *pub, struct lbx_imginfo *info)
469 {
470         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
471
472         *info = (struct lbx_imginfo) {
473                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
474         };
475
476         /* There seems to be two ways of specifying that an image loops. */
477         if (img->flags & FLAG_LOOPING) {
478                 info->loopstart = 0;
479                 info->looping   = 1;
480         } else if (img->leadin != pub->frames - 1) {
481                 info->loopstart = img->leadin;
482                 info->looping   = 1;
483         }
484 }
485
486 unsigned char **lbx_img_getmask(struct lbx_image *pub)
487 {
488         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
489
490         return img->mask;
491 }
492
493 int lbx_img_close(struct lbx_image *pub)
494 {
495         struct lbx_image_priv *img = (struct lbx_image_priv *)pub;
496         int rc = 0;
497
498         if (img) {
499                 if (img->framedata) {
500                         free(img->framedata[0]);
501                         free(img->framedata);
502                 }
503
504                 if (img->mask) {
505                         free(img->mask[0]);
506                         free(img->mask);
507                 }
508
509                 if (img && img->dtor) {
510                         rc = img->dtor(img->f);
511                 }
512
513                 free(img);
514         }
515
516         return rc;
517 }