]> git.draconx.ca Git - liblbx.git/blob - src/image.c
Move reading of embedded palette header into lbximg_fopen().
[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         uint16_t palstart, palcount;
44
45         FILE *f;
46         long foff, paloff;
47
48         int currentframe;
49         int currentx, currenty;
50         unsigned char **framedata;
51         unsigned char **mask;
52
53         uint32_t offsets[];
54 };
55
56 struct lbx_image *lbximg_fopen(FILE *f)
57 {
58         struct lbx_image tmp = {.f = f}, *new = NULL;
59         size_t rc;
60
61         if (fread(&tmp.width,  sizeof tmp.width,   1, f) != 1) goto readerr;
62         if (fread(&tmp.height, sizeof tmp.height,  1, f) != 1) goto readerr;
63         if (fread(&tmp.wtf1,   sizeof tmp.wtf1,    1, f) != 1) goto readerr;
64         if (fread(&tmp.frames, sizeof tmp.frames,  1, f) != 1) goto readerr;
65         if (fread(&tmp.leadin, sizeof tmp.leadin,  1, f) != 1) goto readerr;
66         if (fread(&tmp.flags,  sizeof tmp.flags,   1, f) != 1) goto readerr;
67
68         tmp.width  = letohs(tmp.width);  tmp.foff += sizeof tmp.width;
69         tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
70         tmp.wtf1   = letohs(tmp.wtf1);   tmp.foff += sizeof tmp.wtf1;
71         tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
72         tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
73         tmp.flags  = letohs(tmp.flags);  tmp.foff += sizeof tmp.flags;
74
75         /*
76          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
77          * files which differ from what I believe to be true of all LBX images.
78          * If we never find any exceptions, we can replace the assertions with
79          * assumptions.
80          */
81         _lbx_assert(tmp.wtf1 == 0);
82         _lbx_assert(tmp.frames > tmp.leadin); /* cmbtshp.lbx breaks this. */
83         _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
84
85         new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
86         if (!new) {
87                 lbx_errno = -errno;
88                 return NULL;
89         }
90
91         *new = tmp;
92         new->currentframe = -1;
93
94         rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
95         if (rc < new->frames+1)
96                 goto readerr;
97         new->foff += sizeof *new->offsets * (new->frames+1);
98
99         if (new->flags & FLAG_PALETTE) {
100                 if (fread(&new->palstart, sizeof new->palstart, 1, f) != 1)
101                         goto readerr;
102                 if (fread(&new->palcount, sizeof new->palcount, 1, f) != 1)
103                         goto readerr;
104
105                 new->palstart = letohs(new->palstart);
106                 new->palcount = letohs(new->palcount);
107                 new->foff    += sizeof new->palstart + sizeof new->palcount;
108                 new->paloff   = new->foff;
109
110                 if (new->palstart + new->palcount > 256) {
111                         lbx_errno = LBX_EFORMAT;
112                         free(new);
113                         return NULL;
114                 }
115         }
116
117         return new;
118 readerr:
119         if (feof(f)) {
120                 lbx_errno = LBX_EEOF;
121         } else {
122                 lbx_errno = -errno;
123         }
124
125         free(new);
126         return NULL;
127 }
128
129 static int _lbx_drawrow(int first, struct lbx_image *img)
130 {
131         uint16_t type, yval, count, xval;
132         unsigned char *pos;
133         unsigned char abyss;
134         size_t rc;
135
136         assert(img->framedata);
137         assert(img->mask);
138
139         if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
140         type = letohs(type); img->foff += sizeof type;
141
142         if (first) {
143                 img->currentx = 0;
144                 img->currenty = 0;
145                 type = 0;
146         }
147
148         if (type == 0) {
149                 if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
150                 yval = letohs(yval); img->foff += sizeof yval;
151                 if (yval == 1000)
152                         return 1;
153                 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
154                 count = letohs(count); img->foff += sizeof count;
155                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
156                 xval = letohs(xval); img->foff += sizeof xval;
157                 if (xval == 1000)
158                         return 1;
159
160                 /* Ensure that the row fits in the image. */
161                 if (img->height - img->currenty <= yval || xval >= img->width) {
162                         lbx_errno = LBX_EFORMAT;
163                         return -1;
164                 }
165
166                 img->currenty += yval;
167                 img->currentx  = xval;
168         } else {
169                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
170                 xval = letohs(xval); img->foff += sizeof xval;
171
172                 if (img->width - img->currentx <= xval) {
173                         lbx_errno = LBX_EFORMAT;
174                         return -1;
175                 }
176                 img->currentx += xval;
177
178                 count = type;
179
180         }
181
182         if (count > img->width - img->currentx) {
183                 lbx_errno = LBX_EFORMAT;
184                 return -1;
185         }
186
187         memset(&img->mask[img->currenty][img->currentx], 1, count);
188
189         pos = &img->framedata[img->currenty][img->currentx];
190         rc  = fread(pos, 1, count, img->f);
191         img->currentx += rc;
192         img->foff     += rc;
193
194         if (rc < count)
195                 goto readerr;
196
197         if (count % 2) {
198                 if (fread(&abyss, 1, 1, img->f) != 1)
199                         goto readerr;
200                 img->foff += 1;
201         }
202
203         return 0;
204 readerr:
205         if (feof(img->f)) {
206                 lbx_errno = LBX_EEOF;
207         } else {
208                 lbx_errno = -errno;
209         }
210         return -1;
211 }
212
213 static unsigned char **allocframebuffer(size_t width, size_t height)
214 {
215         unsigned char **new, *tmp;
216         size_t i;
217
218         tmp = calloc(height, width);
219         if (!tmp) {
220                 lbx_errno = -errno;
221                 return NULL;
222         }
223
224         new = malloc(height * sizeof *new);
225         if (!new) {
226                 lbx_errno = -errno;
227                 free(tmp);
228                 return NULL;
229         }
230
231         for (i = 0; i < height; i++) {
232                 new[i] = tmp + i * width;
233         }
234
235         return new;
236 }
237
238 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
239 {
240         if (frame >= img->frames || frame < 0) {
241                 lbx_errno = LBX_ERANGE;
242                 return NULL;
243         }
244
245         if (!img->framedata) {
246                 img->framedata = allocframebuffer(img->width, img->height);
247                 if (!img->framedata)
248                         return NULL;
249         }
250
251         if (!img->mask) {
252                 img->mask = allocframebuffer(img->width, img->height);
253                 if (!img->mask)
254                         return NULL;
255         }
256
257         /* Start over if we are backtracking. */
258         if (img->currentframe > frame)
259                 img->currentframe == -1;
260
261         if (img->flags & FLAG_OVERWRITE) {
262                 /* Clear the slate. */
263                 memset(img->framedata[0], 0, img->width * img->height);
264                 memset(img->mask[0],      0, img->width * img->height);
265         } else {
266                 /* We must have previous frame decoded to continue. */
267                 if (frame > img->currentframe + 1) {
268                         if (!lbximg_getframe(img, frame-1))
269                                 return NULL;
270                 }
271         }
272
273         if (img->currentframe != frame) {
274                 int rc, first = 1;
275
276                 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
277                         return NULL;
278
279                 do {
280                         rc = _lbx_drawrow(first, img);
281                         if (rc == -1)
282                                 return NULL;
283                         first = 0;
284
285                         if (!rc && img->foff > img->offsets[frame+1]) {
286                                 lbx_errno = LBX_EFORMAT;
287                                 return NULL;
288                         }
289                 } while (!rc);
290         }
291
292         img->currentframe = frame;
293         return img->framedata;
294 }
295
296 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
297 {
298         uint8_t entry[4];
299         int i;
300
301         for (i = 0; i < 256; i++) {
302                 if (fread(entry, sizeof entry, 1, f) != 1) {
303                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
304                         return -1;
305                 }
306
307                 if (entry[0] != 1) {
308                         lbx_errno = LBX_EFORMAT;
309                         return -1;
310                 }
311
312                 palette[i] = (struct lbx_colour){
313                         .red   = entry[1] << 2,
314                         .green = entry[2] << 2,
315                         .blue  = entry[3] << 2,
316                 };
317         }
318
319         return 0;
320 }
321
322 int
323 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
324 {
325         unsigned int i;
326         size_t rc;
327
328         uint8_t  entry[4];
329
330         /* Do nothing if the image doesn't have embedded palette data. */
331         if (!(img->flags & FLAG_PALETTE))
332                 return 0;
333
334         if (_lbx_fseek(img->f, &img->foff, img->paloff) == -1)
335                 return -1;
336         
337         for (i = 0; i < img->palcount; 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[img->palstart + 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                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
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 }