]> git.draconx.ca Git - liblbx.git/blob - src/image.c
liblbx: Fix broken logic when decoding earlier image frames.
[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 (C) 2006-2008 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 #ifdef HAVE_CONFIG_H
20 #       include "config.h"
21 #endif
22
23 #include <stdlib.h>
24 #include <string.h>
25 #include <assert.h>
26 #include <errno.h>
27
28 #include "pack.h"
29 #include "misc.h"
30 #include "lbx.h"
31 #include "image.h"
32
33 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
34 #define FLAG_PALETTE   0x1000 /* Image contains embedded palette. */
35 #define FLAG_LOOPING   0x2000 /* Loop over all frames in the image (unsure). */
36 #define FLAG_ALL (FLAG_OVERWRITE|FLAG_PALETTE|FLAG_LOOPING)
37
38 #define HDR_LEN 12
39
40 struct lbx_image {
41         unsigned short width, height;
42         unsigned short wtf, flags;
43         unsigned short frames, leadin;
44         unsigned short palstart, palcount;
45
46         const struct lbx_file_ops *fops;
47         int (*dtor)(void *handle);
48         void *f;
49
50         long paloff;
51
52         int currentframe;
53         int currentx, currenty;
54         unsigned char **framedata;
55         unsigned char **mask;
56
57         unsigned long offsets[];
58 };
59
60 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
61 {
62         unsigned short nframes = unpack_16_le(hdr+6);
63         struct lbx_image *img;
64
65         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
66         if (!img) {
67                 lbx_errno = -errno;
68                 return NULL;
69         }
70
71         *img = (struct lbx_image) {
72                 .width  = unpack_16_le(hdr+0),
73                 .height = unpack_16_le(hdr+2),
74                 .wtf    = unpack_16_le(hdr+4),
75                 .frames = unpack_16_le(hdr+6),
76                 .leadin = unpack_16_le(hdr+8),
77                 .flags  = unpack_16_le(hdr+10),
78
79                 .currentframe = -1,
80         };
81
82         return img;
83 }
84
85 struct lbx_image *lbximg_open(void *f, const struct lbx_file_ops *fops,
86                               int (*destructor)(void *))
87 {
88         unsigned char hdr_buf[HDR_LEN];
89         struct lbx_image *img;
90
91         if (fops->read(hdr_buf, sizeof hdr_buf, f) != sizeof hdr_buf) {
92                 lbx_errno = -errno;
93                 if (fops->eof(f))
94                         lbx_errno = LBX_EEOF;
95                 return NULL;
96         }
97
98         img = lbximg_init(hdr_buf);
99         if (!img)
100                 return NULL;
101
102         img->f    = f;
103         img->fops = fops;
104         img->dtor = destructor;
105
106         /*
107          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
108          * files which differ from what I believe to be true of all LBX images.
109          * If we never find any exceptions, we can replace the assertions with
110          * assumptions.
111          */
112         _lbx_assert(img->wtf == 0); /* version? */
113         _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
114         _lbx_assert(!(img->flags & ~FLAG_ALL));
115
116         /* Read all offsets.  Should be merged with identical code in lbx.c */
117         for (unsigned i = 0; i <= img->frames; i++) {
118                 unsigned char buf[4];
119
120                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
121                         lbx_errno = -errno;
122                         if (fops->eof(f))
123                                 lbx_errno = LBX_EEOF;
124                         free(img);
125                         return NULL;
126                 }
127
128                 img->offsets[i] = unpack_32_le(buf);
129         }
130
131         if (img->flags & FLAG_PALETTE) {
132                 unsigned char buf[4];
133
134                 if (fops->read(buf, sizeof buf, f) != sizeof buf) {
135                         lbx_errno = -errno;
136                         if (fops->eof(f))
137                                 lbx_errno = LBX_EEOF;
138                         free(img);
139                         return NULL;
140                 }
141
142                 img->palstart = unpack_16_le(buf+0);
143                 img->palcount = unpack_16_le(buf+2);
144                 img->paloff   = fops->tell(f);
145
146                 if (img->palstart + img->palcount > 256) {
147                         lbx_errno = LBX_EFORMAT;
148                         free(img);
149                         return NULL;
150                 }
151         }
152
153         return img;
154 }
155
156 struct lbx_image *lbximg_fopen(FILE *f)
157 {
158         return lbximg_open(f, &lbx_default_fops, NULL);
159 }
160
161 static int _lbx_drawrow(int first, struct lbx_image *img)
162 {
163         unsigned short type, count, yval, xval;
164         unsigned char buf[4];
165         unsigned char *pos;
166         size_t rc;
167
168         assert(img->framedata);
169         assert(img->mask);
170
171         if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
172                 goto readerr;
173         type = unpack_16_le(buf+0);
174
175         if (first) {
176                 img->currentx = 0;
177                 img->currenty = 0;
178                 type = 0;
179         }
180
181         if (type == 0) {
182                 yval = unpack_16_le(buf+2);
183                 if (yval == 1000)
184                         return 1;
185
186                 if (img->fops->read(buf, sizeof buf, img->f) != sizeof buf)
187                         goto readerr;
188                 count = unpack_16_le(buf+0);
189
190                 xval = unpack_16_le(buf+2);
191                 if (xval == 1000)
192                         return 1;
193
194                 /* Ensure that the row fits in the image. */
195                 if (img->height - img->currenty <= yval || xval >= img->width) {
196                         lbx_errno = LBX_EFORMAT;
197                         return -1;
198                 }
199
200                 img->currenty += yval;
201                 img->currentx  = xval;
202         } else {
203                 xval = unpack_16_le(buf+2);
204
205                 if (img->width - img->currentx <= xval) {
206                         lbx_errno = LBX_EFORMAT;
207                         return -1;
208                 }
209                 img->currentx += xval;
210
211                 count = type;
212         }
213
214         if (count > img->width - img->currentx) {
215                 lbx_errno = LBX_EFORMAT;
216                 return -1;
217         }
218
219         memset(&img->mask[img->currenty][img->currentx], 1, count);
220
221         pos = &img->framedata[img->currenty][img->currentx];
222         rc  = img->fops->read(pos, count, img->f);
223         img->currentx += rc;
224
225         if (rc < count)
226                 goto readerr;
227
228         if (count % 2) {
229                 if (img->fops->read(buf, 1, img->f) != 1)
230                         goto readerr;
231         }
232
233         return 0;
234 readerr:
235         lbx_errno = -errno;
236         if (img->fops->eof(img->f))
237                 lbx_errno = LBX_EEOF;
238         return -1;
239 }
240
241 static unsigned char **allocframebuffer(size_t width, size_t height)
242 {
243         unsigned char **new, *tmp;
244         size_t i;
245
246         tmp = calloc(height, width);
247         if (!tmp) {
248                 lbx_errno = -errno;
249                 return NULL;
250         }
251
252         new = malloc(height * sizeof *new);
253         if (!new) {
254                 lbx_errno = -errno;
255                 free(tmp);
256                 return NULL;
257         }
258
259         for (i = 0; i < height; i++) {
260                 new[i] = tmp + i * width;
261         }
262
263         return new;
264 }
265
266 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
267 {
268         if (frame >= img->frames || frame < 0) {
269                 lbx_errno = LBX_ERANGE;
270                 return NULL;
271         }
272
273         if (!img->framedata) {
274                 img->framedata = allocframebuffer(img->width, img->height);
275                 if (!img->framedata)
276                         return NULL;
277         }
278
279         if (!img->mask) {
280                 img->mask = allocframebuffer(img->width, img->height);
281                 if (!img->mask)
282                         return NULL;
283         }
284
285         if (img->flags & FLAG_OVERWRITE) {
286                 /* Clear the slate. */
287                 img->currentframe = -1;
288                 memset(img->framedata[0], 0, img->width * img->height);
289                 memset(img->mask[0],      0, img->width * img->height);
290         } else {
291                 /* Start over if we are backtracking. */
292                 if (img->currentframe > frame)
293                         img->currentframe = -1;
294
295                 /* We must have previous frame decoded to continue. */
296                 if (frame > img->currentframe + 1) {
297                         if (!lbximg_getframe(img, frame-1))
298                                 return NULL;
299                 }
300         }
301
302         if (img->currentframe != frame) {
303                 int rc, first = 1;
304
305                 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
306                         lbx_errno = -errno;
307                         return NULL;
308                 }
309
310                 do {
311                         rc = _lbx_drawrow(first, img);
312                         if (rc == -1)
313                                 return NULL;
314                         first = 0;
315
316                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
317                                 lbx_errno = LBX_EFORMAT;
318                                 return NULL;
319                         }
320                 } while (!rc);
321         }
322
323         img->currentframe = frame;
324         return img->framedata;
325 }
326
327 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
328 {
329         unsigned char entry[4];
330         int i;
331
332         for (i = 0; i < 256; i++) {
333                 if (fread(entry, sizeof entry, 1, f) != 1) {
334                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
335                         return -1;
336                 }
337
338                 if (entry[0] != 1) {
339                         lbx_errno = LBX_EFORMAT;
340                         return -1;
341                 }
342
343                 palette[i] = (struct lbx_colour){
344                         .red   = entry[1] << 2,
345                         .green = entry[2] << 2,
346                         .blue  = entry[3] << 2,
347                 };
348         }
349
350         return 0;
351 }
352
353 int
354 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
355 {
356         unsigned char entry[4];
357         unsigned int i;
358         size_t rc;
359
360         /* Do nothing if the image doesn't have embedded palette data. */
361         if (!(img->flags & FLAG_PALETTE))
362                 return 0;
363
364         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
365                 lbx_errno = -errno;
366                 return -1;
367         }
368
369         for (i = 0; i < img->palcount; i++) {
370                 rc = img->fops->read(entry, sizeof entry, img->f);
371
372                 if (rc < sizeof entry) {
373                         goto readerr;
374                 }
375
376                 if (entry[0] != 0) {
377                         lbx_errno = LBX_EFORMAT;
378                         return -1;
379                 }
380
381                 palette[img->palstart + i] = (struct lbx_colour){
382                         .red   = entry[1] << 2,
383                         .green = entry[2] << 2,
384                         .blue  = entry[3] << 2,
385                 };
386         }
387
388         return 0;
389 readerr:
390         lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
391         return -1;
392 }
393
394 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
395 {
396         *info = (struct lbx_imginfo) {
397                 .width      = img->width,
398                 .height     = img->height,
399                 .nframes    = img->frames,
400                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
401         };
402
403         /* There seems to be two ways of specifying that an image loops. */
404         if (img->flags & FLAG_LOOPING) {
405                 info->loopstart = 0;
406                 info->looping   = 1;
407         } else if (img->leadin != img->frames - 1) {
408                 info->loopstart = img->leadin;
409                 info->looping   = 1;
410         }
411 }
412
413 unsigned char **lbximg_getmask(struct lbx_image *img)
414 {
415         return img->mask;
416 }
417
418 int lbximg_close(struct lbx_image *img)
419 {
420         int rc = 0;
421
422         if (img) {
423                 if (img->framedata) {
424                         free(img->framedata[0]);
425                         free(img->framedata);
426                 }
427
428                 if (img->mask) {
429                         free(img->mask[0]);
430                         free(img->mask);
431                 }
432
433                 if (img && img->dtor) {
434                         rc = img->dtor(img->f);
435                 }
436
437                 free(img);
438         }
439
440         return rc;
441 }