]> git.draconx.ca Git - liblbx.git/blob - src/image.c
"Downgrade" the nframes > leadin constraint to a format assertion.
[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         /*
75          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
76          * files which differ from what I believe to be true of all LBX images.
77          * If we never find any exceptions, we can replace the assertions with
78          * assumptions.
79          */
80         _lbx_assert(tmp.wtf1 == 0);
81         _lbx_assert(tmp.frames > tmp.leadin); /* cmbtshp.lbx breaks this. */
82         _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
83
84         new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
85         if (!new) {
86                 lbx_errno = -errno;
87                 return NULL;
88         }
89
90         *new = tmp;
91         new->currentframe = -1;
92
93         rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
94         if (rc < new->frames+1)
95                 goto readerr;
96         new->foff += sizeof *new->offsets * (new->frames+1);
97
98         return new;
99 readerr:
100         if (feof(f)) {
101                 lbx_errno = LBX_EEOF;
102         } else {
103                 lbx_errno = -errno;
104         }
105
106         free(new);
107         return NULL;
108 }
109
110 static int _lbx_drawrow(int first, struct lbx_image *img)
111 {
112         uint16_t type, yval, count, xval;
113         unsigned char *pos;
114         unsigned char abyss;
115         size_t rc;
116
117         assert(img->framedata);
118         assert(img->mask);
119
120         if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
121         type = letohs(type); img->foff += sizeof type;
122
123         if (first) {
124                 img->currentx = 0;
125                 img->currenty = 0;
126                 type = 0;
127         }
128
129         if (type == 0) {
130                 if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
131                 yval = letohs(yval); img->foff += sizeof yval;
132                 if (yval == 1000)
133                         return 1;
134                 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
135                 count = letohs(count); img->foff += sizeof count;
136                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
137                 xval = letohs(xval); img->foff += sizeof xval;
138                 if (xval == 1000)
139                         return 1;
140
141                 /* Ensure that the row fits in the image. */
142                 if (img->height - img->currenty <= yval || xval >= img->width) {
143                         lbx_errno = LBX_EFORMAT;
144                         return -1;
145                 }
146
147                 img->currenty += yval;
148                 img->currentx  = xval;
149         } else {
150                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
151                 xval = letohs(xval); img->foff += sizeof xval;
152
153                 if (img->width - img->currentx <= xval) {
154                         lbx_errno = LBX_EFORMAT;
155                         return -1;
156                 }
157                 img->currentx += xval;
158
159                 count = type;
160
161         }
162
163         if (count > img->width - img->currentx) {
164                 lbx_errno = LBX_EFORMAT;
165                 return -1;
166         }
167
168         memset(&img->mask[img->currenty][img->currentx], 1, count);
169
170         pos = &img->framedata[img->currenty][img->currentx];
171         rc  = fread(pos, 1, count, img->f);
172         img->currentx += rc;
173         img->foff     += rc;
174
175         if (rc < count)
176                 goto readerr;
177
178         if (count % 2) {
179                 if (fread(&abyss, 1, 1, img->f) != 1)
180                         goto readerr;
181                 img->foff += 1;
182         }
183
184         return 0;
185 readerr:
186         if (feof(img->f)) {
187                 lbx_errno = LBX_EEOF;
188         } else {
189                 lbx_errno = -errno;
190         }
191         return -1;
192 }
193
194 static unsigned char **allocframebuffer(size_t width, size_t height)
195 {
196         unsigned char **new, *tmp;
197         size_t i;
198
199         tmp = calloc(height, width);
200         if (!tmp) {
201                 lbx_errno = -errno;
202                 return NULL;
203         }
204
205         new = malloc(height * sizeof *new);
206         if (!new) {
207                 lbx_errno = -errno;
208                 free(tmp);
209                 return NULL;
210         }
211
212         for (i = 0; i < height; i++) {
213                 new[i] = tmp + i * width;
214         }
215
216         return new;
217 }
218
219 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
220 {
221         if (frame >= img->frames || frame < 0) {
222                 lbx_errno = LBX_ERANGE;
223                 return NULL;
224         }
225
226         if (!img->framedata) {
227                 img->framedata = allocframebuffer(img->width, img->height);
228                 if (!img->framedata)
229                         return NULL;
230         }
231
232         if (!img->mask) {
233                 img->mask = allocframebuffer(img->width, img->height);
234                 if (!img->mask)
235                         return NULL;
236         }
237
238         /* Start over if we are backtracking. */
239         if (img->currentframe > frame)
240                 img->currentframe == -1;
241
242         if (img->flags & FLAG_OVERWRITE) {
243                 /* Clear the slate. */
244                 memset(img->framedata[0], 0, img->width * img->height);
245                 memset(img->mask[0],      0, img->width * img->height);
246         } else {
247                 /* We must have previous frame decoded to continue. */
248                 if (frame > img->currentframe + 1) {
249                         if (!lbximg_getframe(img, frame-1))
250                                 return NULL;
251                 }
252         }
253
254         if (img->currentframe != frame) {
255                 int rc, first = 1;
256
257                 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
258                         return NULL;
259
260                 do {
261                         rc = _lbx_drawrow(first, img);
262                         if (rc == -1)
263                                 return NULL;
264                         first = 0;
265
266                         if (!rc && img->foff > img->offsets[frame+1]) {
267                                 lbx_errno = LBX_EFORMAT;
268                                 return NULL;
269                         }
270                 } while (!rc);
271         }
272
273         img->currentframe = frame;
274         return img->framedata;
275 }
276
277 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
278 {
279         uint8_t entry[4];
280         int i;
281
282         for (i = 0; i < 256; i++) {
283                 if (fread(entry, sizeof entry, 1, f) != 1) {
284                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
285                         return -1;
286                 }
287
288                 if (entry[0] != 1) {
289                         lbx_errno = LBX_EFORMAT;
290                         return -1;
291                 }
292
293                 palette[i] = (struct lbx_colour){
294                         .red   = entry[1] << 2,
295                         .green = entry[2] << 2,
296                         .blue  = entry[3] << 2,
297                 };
298         }
299
300         return 0;
301 }
302
303 int
304 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
305 {
306         size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
307         unsigned int i;
308         size_t rc;
309
310         uint16_t start, count;
311         uint8_t  entry[4];
312
313         /* Do nothing if the image doesn't have embedded palette data. */
314         if (!(img->flags & FLAG_PALETTE))
315                 return 0;
316
317         /* Palette data is located right after the header. */
318         if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
319                 return -1;
320         
321         /* Palette header */
322         if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
323         if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
324         start = letohs(start); img->foff += sizeof start;
325         count = letohs(count); img->foff += sizeof count;
326
327         if (start + count > 256) {
328                 lbx_errno = LBX_EFORMAT;
329                 return -1;
330         }
331
332         if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
333                 lbx_errno = LBX_EFORMAT;
334                 return -1;
335         }
336
337         for (i = 0; i < count; i++) {
338                 rc = fread(entry, 1, sizeof entry, img->f);
339                 img->foff += rc;
340
341                 if (rc < sizeof entry) {
342                         goto readerr;
343                 }
344
345                 if (entry[0] != 0) {
346                         lbx_errno = LBX_EFORMAT;
347                         return -1;
348                 }
349
350                 palette[start + i] = (struct lbx_colour){
351                         .red   = entry[1] << 2,
352                         .green = entry[2] << 2,
353                         .blue  = entry[3] << 2,
354                 };
355         }
356
357         return 0;
358 readerr:
359         lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
360         return -1;
361 }
362
363 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
364 {
365         *info = (struct lbx_imginfo) {
366                 .width      = img->width,
367                 .height     = img->height,
368                 .nframes    = img->frames,
369                 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
370         };
371
372         /* There seems to be two ways of specifying that an image loops. */
373         if (img->flags & FLAG_LOOPING) {
374                 info->loopstart = 0;
375                 info->looping   = 1;
376         } else if (img->leadin != img->frames - 1) {
377                 info->loopstart = img->leadin;
378                 info->looping   = 1;
379         }
380 }
381
382 unsigned char **lbximg_getmask(struct lbx_image *img)
383 {
384         return img->mask;
385 }
386
387 void lbximg_close(struct lbx_image *img)
388 {
389         if (!img) return;
390
391         if (img->framedata) {
392                 free(img->framedata[0]);
393                 free(img->framedata);
394         }
395
396         if (img->mask) {
397                 free(img->mask[0]);
398                 free(img->mask);
399         }
400
401         if (img->f) {
402                 fclose(img->f);
403         }
404
405         free(img);
406 }