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