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