]> git.draconx.ca Git - liblbx.git/blob - src/image.c
Starting implementation of image parsing code.
[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;
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         /*
49          * DEBUG ONLY.  These assertions exist to catch otherwise valid image
50          * files which differ from what I believe to be true of all LBX images.
51          * If we never find any exceptions, we can replace the assertions with
52          * assumptions.
53          */
54         assert(tmp.wtf1 == 0);
55         assert(tmp.offs == tmp.frames + 1);
56
57         new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
58         if (!new) {
59                 lbx_errno = -errno;
60                 return NULL;
61         }
62
63         *new = tmp;
64         new->currentframe = -1;
65
66         if (fread(new->offsets, sizeof *new->offsets, new->offs, f)!= new->offs)
67                 goto readerr;
68         new->foff += sizeof *new->offsets * new->offs;
69
70         return new;
71 readerr:
72         if (feof(f)) {
73                 lbx_errno = LBX_EEOF;
74         } else {
75                 lbx_errno = -errno;
76         }
77
78         free(new);
79         return NULL;
80 }
81
82 static int _lbx_drawrow(int first, struct lbx_image *img)
83 {
84         uint16_t type, yval, count, xval;
85         unsigned char *pos;
86         unsigned char abyss;
87         size_t rc;
88
89         assert(img->framedata);
90
91         if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
92         type = letohs(type); img->foff += sizeof type;
93
94         if (first) {
95                 img->currentx = 0;
96                 img->currenty = 0;
97                 type = 0;
98         }
99
100         if (type == 0) {
101                 if (fread(&yval,  sizeof yval,  1, img->f) != 1) goto readerr;
102                 yval = letohs(yval); img->foff += sizeof yval;
103
104                 if (yval == 1000)
105                         return 1;
106
107                 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
108                 count = letohs(count); img->foff += sizeof count;
109                 if (fread(&xval,  sizeof xval,  1, img->f) != 1) goto readerr;
110                 xval = letohs(xval); img->foff += sizeof xval;
111
112                 /* Ensure that the row fits in the image. */
113                 if (img->height - img->currenty <= yval
114                     || xval >= img->width || count > img->width - xval) {
115                         lbx_errno = LBX_EFORMAT;
116                         return -1;
117                 }
118
119                 img->currenty += yval;
120                 img->currentx  = xval;
121         } else {
122                 count = 0;
123 /*
124                 count = type;
125                 if (count > img->width - img->currentx) {
126                         lbx_errno = LBX_EFORMAT;
127                         return -1;
128                 }
129 */
130         }
131
132         pos = &img->framedata[img->currenty][img->currentx];
133         rc  = fread(pos, 1, count, img->f);
134         img->currentx += rc;
135         img->foff     += rc;
136
137         if (rc < count)
138                 goto readerr;
139
140         if (count % 2) {
141                 if (fread(&abyss, 1, 1, img->f) != 1)
142                         goto readerr;
143                 img->foff += 1;
144         }
145
146         return 0;
147 readerr:
148         if (feof(img->f)) {
149                 lbx_errno = LBX_EEOF;
150         } else {
151                 lbx_errno = -errno;
152         }
153         return -1;
154 }
155
156 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
157 {
158         if (frame >= img->frames || frame < 0) {
159                 lbx_errno = LBX_ERANGE;
160                 return NULL;
161         }
162
163         if (!img->framedata) {
164                 unsigned char *tmp;
165                 int i;
166
167                 tmp = malloc(img->width * img->height);
168                 if (!tmp) {
169                         lbx_errno = -errno;
170                         return NULL;
171                 }
172
173                 img->framedata = malloc(img->height * sizeof *img->framedata);
174                 if (!img->framedata) {
175                         lbx_errno = -errno;
176                         free(tmp);
177                         return NULL;
178                 }
179
180                 for (i = 0; i < img->height; i++) {
181                         img->framedata[i] = tmp + i * img->width;
182                 }
183         }
184
185         /* Start over if we are backtracking. */
186         if (img->currentframe > frame)
187                 img->currentframe == -1;
188
189         /* We must have previous frame decoded to continue. */
190         if (frame > img->currentframe + 1) {
191                 if (!lbximg_getframe(img, frame-1))
192                         return NULL;
193         }
194
195         if (img->currentframe != frame) {
196                 int rc, first = 1;
197
198                 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
199                         return NULL;
200
201                 do {
202                         rc = _lbx_drawrow(first, img);
203                         if (rc == -1)
204                                 return NULL;
205                         first = 0;
206
207                         if (img->foff > img->offsets[frame+1]) {
208                                 lbx_errno = LBX_EFORMAT;
209                                 return NULL;
210                         }
211                 } while (!rc);
212         }
213
214         img->currentframe = frame;
215         return img->framedata;
216 }
217
218 void lbximg_close(struct lbx_image *img)
219 {
220         if (!img) return;
221
222         if (img->framedata) {
223                 free(img->framedata[0]);
224                 free(img->framedata);
225         }
226
227         if (img->f) {
228                 fclose(img->f);
229         }
230
231         free(img);
232 }