]> git.draconx.ca Git - liblbx.git/blob - src/image.c
Update license information.
[liblbx.git] / src / image.c
1 /* 2ooM: The Master of Orion II Reverse Engineering Project
2  * Library for working with LBX image files.
3  * Copyright (C) 2006-2008 Nick Bowler
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 2 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
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 "misc.h"
31 #include "lbx.h"
32 #include "image.h"
33
34 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
35 #define FLAG_PALETTE   0x1000 /* Image contains embedded palette. */
36 #define FLAG_LOOPING   0x2000 /* Loop over all frames in the image (unsure). */
37
38 struct lbx_image {
39         uint16_t width, height;
40         uint16_t wtf1;
41         uint16_t frames, leadin;
42         uint16_t flags;
43
44         FILE *f;
45         long foff;
46
47         int currentframe;
48         int currentx, currenty;
49         unsigned char **framedata;
50         unsigned char **mask;
51
52         uint32_t offsets[];
53 };
54
55 struct lbx_image *lbximg_fopen(FILE *f)
56 {
57         struct lbx_image tmp = {.f = f}, *new = NULL;
58         size_t rc;
59
60         if (fread(&tmp.width,  sizeof tmp.width,   1, f) != 1) goto readerr;
61         if (fread(&tmp.height, sizeof tmp.height,  1, f) != 1) goto readerr;
62         if (fread(&tmp.wtf1,   sizeof tmp.wtf1,    1, f) != 1) goto readerr;
63         if (fread(&tmp.frames, sizeof tmp.frames,  1, f) != 1) goto readerr;
64         if (fread(&tmp.leadin, sizeof tmp.leadin,  1, f) != 1) goto readerr;
65         if (fread(&tmp.flags,  sizeof tmp.flags,   1, f) != 1) goto readerr;
66
67         tmp.width  = letohs(tmp.width);  tmp.foff += sizeof tmp.width;
68         tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
69         tmp.wtf1   = letohs(tmp.wtf1);   tmp.foff += sizeof tmp.wtf1;
70         tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
71         tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
72         tmp.flags  = letohs(tmp.flags);  tmp.foff += sizeof tmp.flags;
73
74         /* Format constraints. */
75         if (tmp.frames <= tmp.leadin) {
76                 lbx_errno = LBX_EFORMAT;
77                 return NULL;
78         }
79
80         /*
81          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
82          * files which differ from what I believe to be true of all LBX images.
83          * If we never find any exceptions, we can replace the assertions with
84          * assumptions.
85          */
86         _lbx_assert(tmp.wtf1 == 0);
87         _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
88
89         new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
90         if (!new) {
91                 lbx_errno = -errno;
92                 return NULL;
93         }
94
95         *new = tmp;
96         new->currentframe = -1;
97
98         rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
99         if (rc < new->frames+1)
100                 goto readerr;
101         new->foff += sizeof *new->offsets * (new->frames+1);
102
103         return new;
104 readerr:
105         if (feof(f)) {
106                 lbx_errno = LBX_EEOF;
107         } else {
108                 lbx_errno = -errno;
109         }
110
111         free(new);
112         return NULL;
113 }
114
115 static int _lbx_drawrow(int first, struct lbx_image *img)
116 {
117         uint16_t type, yval, count, xval;
118         unsigned char *pos;
119         unsigned char abyss;
120         size_t rc;
121
122         assert(img->framedata);
123         assert(img->mask);
124
125         if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
126         type = letohs(type); img->foff += sizeof type;
127
128         if (first) {
129                 img->currentx = 0;
130                 img->currenty = 0;
131                 type = 0;
132         }
133
134         if (type == 0) {
135                 if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
136                 yval = letohs(yval); img->foff += sizeof yval;
137                 if (yval == 1000)
138                         return 1;
139                 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
140                 count = letohs(count); img->foff += sizeof count;
141                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
142                 xval = letohs(xval); img->foff += sizeof xval;
143                 if (xval == 1000)
144                         return 1;
145
146                 /* Ensure that the row fits in the image. */
147                 if (img->height - img->currenty <= yval || xval >= img->width) {
148                         lbx_errno = LBX_EFORMAT;
149                         return -1;
150                 }
151
152                 img->currenty += yval;
153                 img->currentx  = xval;
154         } else {
155                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
156                 xval = letohs(xval); img->foff += sizeof xval;
157
158                 if (img->width - img->currentx <= xval) {
159                         lbx_errno = LBX_EFORMAT;
160                         return -1;
161                 }
162                 img->currentx += xval;
163
164                 count = type;
165
166         }
167
168         if (count > img->width - img->currentx) {
169                 lbx_errno = LBX_EFORMAT;
170                 return -1;
171         }
172
173         memset(&img->mask[img->currenty][img->currentx], 1, count);
174
175         pos = &img->framedata[img->currenty][img->currentx];
176         rc  = fread(pos, 1, count, img->f);
177         img->currentx += rc;
178         img->foff     += rc;
179
180         if (rc < count)
181                 goto readerr;
182
183         if (count % 2) {
184                 if (fread(&abyss, 1, 1, img->f) != 1)
185                         goto readerr;
186                 img->foff += 1;
187         }
188
189         return 0;
190 readerr:
191         if (feof(img->f)) {
192                 lbx_errno = LBX_EEOF;
193         } else {
194                 lbx_errno = -errno;
195         }
196         return -1;
197 }
198
199 static unsigned char **allocframebuffer(size_t width, size_t height)
200 {
201         unsigned char **new, *tmp;
202         size_t i;
203
204         tmp = calloc(height, width);
205         if (!tmp) {
206                 lbx_errno = -errno;
207                 return NULL;
208         }
209
210         new = malloc(height * sizeof *new);
211         if (!new) {
212                 lbx_errno = -errno;
213                 free(tmp);
214                 return NULL;
215         }
216
217         for (i = 0; i < height; i++) {
218                 new[i] = tmp + i * width;
219         }
220
221         return new;
222 }
223
224 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
225 {
226         if (frame >= img->frames || frame < 0) {
227                 lbx_errno = LBX_ERANGE;
228                 return NULL;
229         }
230
231         if (!img->framedata) {
232                 img->framedata = allocframebuffer(img->width, img->height);
233                 if (!img->framedata)
234                         return NULL;
235         }
236
237         if (!img->mask) {
238                 img->mask = allocframebuffer(img->width, img->height);
239                 if (!img->mask)
240                         return NULL;
241         }
242
243         /* Start over if we are backtracking. */
244         if (img->currentframe > frame)
245                 img->currentframe == -1;
246
247         if (img->flags & FLAG_OVERWRITE) {
248                 /* Clear the slate. */
249                 memset(img->framedata[0], 0, img->width * img->height);
250                 memset(img->mask[0],      0, img->width * img->height);
251         } else {
252                 /* We must have previous frame decoded to continue. */
253                 if (frame > img->currentframe + 1) {
254                         if (!lbximg_getframe(img, frame-1))
255                                 return NULL;
256                 }
257         }
258
259         if (img->currentframe != frame) {
260                 int rc, first = 1;
261
262                 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
263                         return NULL;
264
265                 do {
266                         rc = _lbx_drawrow(first, img);
267                         if (rc == -1)
268                                 return NULL;
269                         first = 0;
270
271                         if (!rc && img->foff > img->offsets[frame+1]) {
272                                 lbx_errno = LBX_EFORMAT;
273                                 return NULL;
274                         }
275                 } while (!rc);
276         }
277
278         img->currentframe = frame;
279         return img->framedata;
280 }
281
282 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
283 {
284         uint8_t entry[4];
285         int i;
286
287         for (i = 0; i < 256; i++) {
288                 if (fread(entry, sizeof entry, 1, f) != 1) {
289                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
290                         return -1;
291                 }
292
293                 if (entry[0] != 1) {
294                         lbx_errno = LBX_EFORMAT;
295                         return -1;
296                 }
297
298                 palette[i] = (struct lbx_colour){
299                         .red   = entry[1] << 2,
300                         .green = entry[2] << 2,
301                         .blue  = entry[3] << 2,
302                 };
303         }
304
305         return 0;
306 }
307
308 int
309 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
310 {
311         size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
312         unsigned int i;
313         size_t rc;
314
315         uint16_t start, count;
316         uint8_t  entry[4];
317
318         /* Do nothing if the image doesn't have embedded palette data. */
319         if (!(img->flags & FLAG_PALETTE))
320                 return 0;
321
322         /* Palette data is located right after the header. */
323         if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
324                 return -1;
325         
326         /* Palette header */
327         if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
328         if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
329         start = letohs(start); img->foff += sizeof start;
330         count = letohs(count); img->foff += sizeof count;
331
332         if (start + count > 256) {
333                 lbx_errno = LBX_EFORMAT;
334                 return -1;
335         }
336
337         if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
338                 lbx_errno = LBX_EFORMAT;
339                 return -1;
340         }
341
342         for (i = 0; i < count; i++) {
343                 rc = fread(entry, 1, sizeof entry, img->f);
344                 img->foff += rc;
345
346                 if (rc < sizeof entry) {
347                         goto readerr;
348                 }
349
350                 if (entry[0] != 0) {
351                         lbx_errno = LBX_EFORMAT;
352                         return -1;
353                 }
354
355                 palette[start + i] = (struct lbx_colour){
356                         .red   = entry[1] << 2,
357                         .green = entry[2] << 2,
358                         .blue  = entry[3] << 2,
359                 };
360         }
361
362         return 0;
363 readerr:
364         lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
365         return -1;
366 }
367
368 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
369 {
370         *info = (struct lbx_imginfo) {
371                 .width      = img->width,
372                 .height     = img->height,
373                 .nframes    = img->frames,
374                 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
375         };
376
377         /* There seems to be two ways of specifying that an image loops. */
378         if (img->flags & FLAG_LOOPING) {
379                 info->loopstart = 0;
380                 info->looping   = 1;
381         } else if (img->leadin != img->frames - 1) {
382                 info->loopstart = img->leadin;
383                 info->looping   = 1;
384         }
385 }
386
387 unsigned char **lbximg_getmask(struct lbx_image *img)
388 {
389         return img->mask;
390 }
391
392 void lbximg_close(struct lbx_image *img)
393 {
394         if (!img) return;
395
396         if (img->framedata) {
397                 free(img->framedata[0]);
398                 free(img->framedata);
399         }
400
401         if (img->mask) {
402                 free(img->mask[0]);
403                 free(img->mask);
404         }
405
406         if (img->f) {
407                 fclose(img->f);
408         }
409
410         free(img);
411 }