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