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