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