10 #include "byteorder.h"
16 uint16_t width, height;
18 uint16_t offs, frames;
24 int currentx, currenty;
25 unsigned char **framedata;
30 struct lbx_image *lbximg_fopen(FILE *f)
32 struct lbx_image tmp = {.f = f}, *new = NULL;
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;
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;
48 /* For some reason, the format seems to need this. */
52 if (tmp.offs <= tmp.frames) {
53 lbx_errno = LBX_EFORMAT;
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
63 _lbx_assert(tmp.wtf1 == 0);
64 _lbx_assert(tmp.offs == tmp.frames + 1);
66 new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
73 new->currentframe = -1;
75 if (fread(new->offsets, sizeof *new->offsets, new->offs, f)!= new->offs)
77 new->foff += sizeof *new->offsets * new->offs;
91 static int _lbx_drawrow(int first, struct lbx_image *img)
93 uint16_t type, yval, count, xval;
98 assert(img->framedata);
100 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
101 type = letohs(type); img->foff += sizeof type;
110 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
111 yval = letohs(yval); img->foff += sizeof yval;
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;
121 /* Ensure that the row fits in the image. */
122 if (img->height - img->currenty <= yval || xval >= img->width) {
123 lbx_errno = LBX_EFORMAT;
127 img->currenty += yval;
128 img->currentx = xval;
130 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
131 xval = letohs(xval); img->foff += sizeof xval;
133 if (img->width - img->currentx <= xval) {
134 lbx_errno = LBX_EFORMAT;
137 img->currentx += xval;
143 if (count > img->width - img->currentx) {
144 lbx_errno = LBX_EFORMAT;
148 pos = &img->framedata[img->currenty][img->currentx];
149 rc = fread(pos, 1, count, img->f);
157 if (fread(&abyss, 1, 1, img->f) != 1)
165 lbx_errno = LBX_EEOF;
172 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
174 if (frame >= img->frames || frame < 0) {
175 lbx_errno = LBX_ERANGE;
179 if (!img->framedata) {
183 tmp = malloc(img->width * img->height);
189 img->framedata = malloc(img->height * sizeof *img->framedata);
190 if (!img->framedata) {
196 for (i = 0; i < img->height; i++) {
197 img->framedata[i] = tmp + i * img->width;
201 /* Start over if we are backtracking. */
202 if (img->currentframe > frame)
203 img->currentframe == -1;
205 /* We must have previous frame decoded to continue. */
206 if (frame > img->currentframe + 1) {
207 if (!lbximg_getframe(img, frame-1))
211 if (img->currentframe != frame) {
214 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
218 rc = _lbx_drawrow(first, img);
223 if (!rc && img->foff > img->offsets[frame+1]) {
224 lbx_errno = LBX_EFORMAT;
230 img->currentframe = frame;
231 return img->framedata;
234 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
239 for (i = 0; i < 256; i++) {
240 if (fread(entry, sizeof entry, 1, f) != 1) {
241 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
246 lbx_errno = LBX_EFORMAT;
250 palette[i] = (struct lbx_colour){
251 .red = entry[1] << 2,
252 .green = entry[2] << 2,
253 .blue = entry[3] << 2,
261 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
265 size_t hdrlen = 6*(sizeof img->width)
266 + (img->offs)*(sizeof *img->offsets);
270 /* Palette data is located right after the header. */
271 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
274 while (img->foff + sizeof entry <= img->offsets[0]) {
275 rc = fread(entry, 1, sizeof entry, img->f);
278 if (rc < sizeof entry) {
285 lbx_errno = LBX_EFORMAT;
292 palette[index] = (struct lbx_colour){
293 .red = entry[1] << 2,
294 .green = entry[2] << 2,
295 .blue = entry[3] << 2,
302 lbx_errno = LBX_EEOF;
308 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
310 *info = (struct lbx_imginfo) {
312 .height = img->height,
313 .nframes = img->frames,
317 void lbximg_close(struct lbx_image *img)
321 if (img->framedata) {
322 free(img->framedata[0]);
323 free(img->framedata);