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