]> git.draconx.ca Git - liblbx.git/blob - src/image.c
liblbx: Remove useless use of uint8_t.
[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         FILE *f;
47         long foff, paloff;
48
49         int currentframe;
50         int currentx, currenty;
51         unsigned char **framedata;
52         unsigned char **mask;
53
54         unsigned long offsets[];
55 };
56
57 static struct lbx_image *lbximg_init(unsigned char hdr[static HDR_LEN])
58 {
59         unsigned short nframes = unpack_16_le(hdr+6);
60         struct lbx_image *img;
61
62         img = malloc(sizeof *img + sizeof img->offsets[0] * (nframes+1));
63         if (!img) {
64                 lbx_errno = -errno;
65                 return NULL;
66         }
67
68         *img = (struct lbx_image) {
69                 .width  = unpack_16_le(hdr+0),
70                 .height = unpack_16_le(hdr+2),
71                 .wtf    = unpack_16_le(hdr+4),
72                 .frames = unpack_16_le(hdr+6),
73                 .leadin = unpack_16_le(hdr+8),
74                 .flags  = unpack_16_le(hdr+10),
75
76                 .currentframe = -1,
77         };
78
79         return img;
80 }
81
82 struct lbx_image *lbximg_fopen(FILE *f)
83 {
84         unsigned char hdr_buf[HDR_LEN];
85         struct lbx_image *img;
86         size_t rc;
87
88         if (fread(hdr_buf, 1, sizeof hdr_buf, f) != sizeof hdr_buf) {
89                 lbx_errno = -errno;
90                 if (feof(f))
91                         lbx_errno = LBX_EEOF;
92                 return NULL;
93         }
94
95         img = lbximg_init(hdr_buf);
96         if (!img)
97                 return NULL;
98
99         img->f    = f;
100         img->foff = sizeof hdr_buf;
101
102         /*
103          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
104          * files which differ from what I believe to be true of all LBX images.
105          * If we never find any exceptions, we can replace the assertions with
106          * assumptions.
107          */
108         _lbx_assert(img->wtf == 0); /* version? */
109         _lbx_assert(img->frames > img->leadin); /* cmbtshp.lbx breaks this. */
110         _lbx_assert(!(img->flags & ~FLAG_ALL));
111
112         /* Read all offsets.  Should be merged with identical code in lbx.c */
113         for (unsigned i = 0; i <= img->frames; i++) {
114                 unsigned char buf[4];
115
116                 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
117                         lbx_errno = -errno;
118                         if (feof(f))
119                                 lbx_errno = LBX_EEOF;
120                         free(img);
121                         return NULL;
122                 }
123
124                 img->offsets[i] = unpack_32_le(buf);
125                 img->foff += 4;
126         }
127
128         if (img->flags & FLAG_PALETTE) {
129                 unsigned char buf[4];
130
131                 if (fread(buf, 1, sizeof buf, f) != sizeof buf) {
132                         lbx_errno = -errno;
133                         if (feof(f))
134                                 lbx_errno = LBX_EEOF;
135                         free(img);
136                         return NULL;
137                 }
138
139                 img->palstart = unpack_16_le(buf+0);
140                 img->palcount = unpack_16_le(buf+2);
141                 img->foff    += sizeof buf;
142                 img->paloff   = img->foff;
143
144                 if (img->palstart + img->palcount > 256) {
145                         lbx_errno = LBX_EFORMAT;
146                         free(img);
147                         return NULL;
148                 }
149         }
150
151         return img;
152 }
153
154 static int _lbx_drawrow(int first, struct lbx_image *img)
155 {
156         unsigned short type, count, yval, xval;
157         unsigned char buf[4];
158         unsigned char *pos;
159         size_t rc;
160
161         assert(img->framedata);
162         assert(img->mask);
163
164         if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
165                 goto readerr;
166         img->foff += 4;
167         type = unpack_16_le(buf+0);
168
169         if (first) {
170                 img->currentx = 0;
171                 img->currenty = 0;
172                 type = 0;
173         }
174
175         if (type == 0) {
176                 yval = unpack_16_le(buf+2);
177                 if (yval == 1000)
178                         return 1;
179
180                 if (fread(buf, 1, sizeof buf, img->f) != sizeof buf)
181                         goto readerr;
182                 img->foff += 4;
183                 count = unpack_16_le(buf+0);
184
185                 xval = unpack_16_le(buf+2);
186                 if (xval == 1000)
187                         return 1;
188
189                 /* Ensure that the row fits in the image. */
190                 if (img->height - img->currenty <= yval || xval >= img->width) {
191                         lbx_errno = LBX_EFORMAT;
192                         return -1;
193                 }
194
195                 img->currenty += yval;
196                 img->currentx  = xval;
197         } else {
198                 xval = unpack_16_le(buf+2);
199
200                 if (img->width - img->currentx <= xval) {
201                         lbx_errno = LBX_EFORMAT;
202                         return -1;
203                 }
204                 img->currentx += xval;
205
206                 count = type;
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(buf, 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         unsigned char 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 char entry[4];
353         unsigned int i;
354         size_t rc;
355
356         /* Do nothing if the image doesn't have embedded palette data. */
357         if (!(img->flags & FLAG_PALETTE))
358                 return 0;
359
360         if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
361                 return -1;
362
363         for (i = 0; i < img->palcount; i++) {
364                 rc = fread(entry, 1, sizeof entry, img->f);
365                 img->foff += rc;
366
367                 if (rc < sizeof entry) {
368                         goto readerr;
369                 }
370
371                 if (entry[0] != 0) {
372                         lbx_errno = LBX_EFORMAT;
373                         return -1;
374                 }
375
376                 palette[img->palstart + i] = (struct lbx_colour){
377                         .red   = entry[1] << 2,
378                         .green = entry[2] << 2,
379                         .blue  = entry[3] << 2,
380                 };
381         }
382
383         return 0;
384 readerr:
385         lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
386         return -1;
387 }
388
389 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
390 {
391         *info = (struct lbx_imginfo) {
392                 .width      = img->width,
393                 .height     = img->height,
394                 .nframes    = img->frames,
395                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
396         };
397
398         /* There seems to be two ways of specifying that an image loops. */
399         if (img->flags & FLAG_LOOPING) {
400                 info->loopstart = 0;
401                 info->looping   = 1;
402         } else if (img->leadin != img->frames - 1) {
403                 info->loopstart = img->leadin;
404                 info->looping   = 1;
405         }
406 }
407
408 unsigned char **lbximg_getmask(struct lbx_image *img)
409 {
410         return img->mask;
411 }
412
413 void lbximg_close(struct lbx_image *img)
414 {
415         if (!img) return;
416
417         if (img->framedata) {
418                 free(img->framedata[0]);
419                 free(img->framedata);
420         }
421
422         if (img->mask) {
423                 free(img->mask[0]);
424                 free(img->mask);
425         }
426
427         if (img->f) {
428                 fclose(img->f);
429         }
430
431         free(img);
432 }