]> git.draconx.ca Git - liblbx.git/blob - src/image.c
76fbfed3528ccf2d8561c7ee1bf9502e7d6eef99
[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                         memset(img->mask[0], 0, img->width * img->height);
296                         img->currentframe = -1;
297                 }
298
299                 /* We must have previous frame decoded to continue. */
300                 if (frame > img->currentframe + 1) {
301                         if (!lbximg_getframe(img, frame-1))
302                                 return NULL;
303                 }
304         }
305
306         if (img->currentframe != frame) {
307                 int rc, first = 1;
308
309                 if (img->fops->seek(img->f, img->offsets[frame], SEEK_SET)) {
310                         lbx_errno = -errno;
311                         return NULL;
312                 }
313
314                 do {
315                         rc = _lbx_drawrow(first, img);
316                         if (rc == -1)
317                                 return NULL;
318                         first = 0;
319
320                         if (img->fops->tell(img->f) > img->offsets[frame+1]) {
321                                 lbx_errno = LBX_EFORMAT;
322                                 return NULL;
323                         }
324                 } while (!rc);
325         }
326
327         img->currentframe = frame;
328         return img->framedata;
329 }
330
331 int
332 lbximg_loadpalette(void *f, const struct lbx_file_ops *fops,
333                    struct lbx_colour palette[static 256])
334 {
335         unsigned char entry[4];
336         int i;
337
338         for (i = 0; i < 256; i++) {
339                 if (fops->read(entry, sizeof entry, f) != sizeof entry) {
340                         lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
341                         return -1;
342                 }
343
344                 if (entry[0] != 1) {
345                         lbx_errno = LBX_EFORMAT;
346                         return -1;
347                 }
348
349                 palette[i] = (struct lbx_colour) {
350                         .red    = entry[1] << 2,
351                         .green  = entry[2] << 2,
352                         .blue   = entry[3] << 2,
353                         .active = 1,
354                 };
355         }
356
357         return 0;
358 }
359
360 int
361 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
362 {
363         unsigned char entry[4];
364         unsigned int i;
365         size_t rc;
366
367         /* Do nothing if the image doesn't have embedded palette data. */
368         if (!(img->flags & FLAG_PALETTE))
369                 return 0;
370
371         if (img->fops->seek(img->f, img->paloff, SEEK_SET)) {
372                 lbx_errno = -errno;
373                 return -1;
374         }
375
376         for (i = 0; i < img->palcount; i++) {
377                 rc = img->fops->read(entry, sizeof entry, img->f);
378                 if (rc < sizeof entry) {
379                         goto readerr;
380                 }
381
382                 if (entry[0] != 0) {
383                         lbx_errno = LBX_EFORMAT;
384                         return -1;
385                 }
386
387                 palette[img->palstart + i] = (struct lbx_colour){
388                         .red    = entry[1] << 2,
389                         .green  = entry[2] << 2,
390                         .blue   = entry[3] << 2,
391                         .active = 1,
392                 };
393         }
394
395         return 0;
396 readerr:
397         lbx_errno = img->fops->eof(img->f) ? LBX_EEOF : -errno;
398         return -1;
399 }
400
401 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
402 {
403         *info = (struct lbx_imginfo) {
404                 .width      = img->width,
405                 .height     = img->height,
406                 .nframes    = img->frames,
407                 .palettesz  = (img->flags & FLAG_PALETTE) ? img->palcount : 0,
408         };
409
410         /* There seems to be two ways of specifying that an image loops. */
411         if (img->flags & FLAG_LOOPING) {
412                 info->loopstart = 0;
413                 info->looping   = 1;
414         } else if (img->leadin != img->frames - 1) {
415                 info->loopstart = img->leadin;
416                 info->looping   = 1;
417         }
418 }
419
420 unsigned char **lbximg_getmask(struct lbx_image *img)
421 {
422         return img->mask;
423 }
424
425 int lbximg_close(struct lbx_image *img)
426 {
427         int rc = 0;
428
429         if (img) {
430                 if (img->framedata) {
431                         free(img->framedata[0]);
432                         free(img->framedata);
433                 }
434
435                 if (img->mask) {
436                         free(img->mask[0]);
437                         free(img->mask);
438                 }
439
440                 if (img && img->dtor) {
441                         rc = img->dtor(img->f);
442                 }
443
444                 free(img);
445         }
446
447         return rc;
448 }