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;
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;
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
54 assert(tmp.wtf1 == 0);
55 assert(tmp.offs == tmp.frames + 1);
57 new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
64 new->currentframe = -1;
66 if (fread(new->offsets, sizeof *new->offsets, new->offs, f)!= new->offs)
68 new->foff += sizeof *new->offsets * new->offs;
82 static int _lbx_drawrow(int first, struct lbx_image *img)
84 uint16_t type, yval, count, xval;
89 assert(img->framedata);
91 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
92 type = letohs(type); img->foff += sizeof type;
101 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
102 yval = letohs(yval); img->foff += sizeof yval;
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;
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;
119 img->currenty += yval;
120 img->currentx = xval;
125 if (count > img->width - img->currentx) {
126 lbx_errno = LBX_EFORMAT;
132 pos = &img->framedata[img->currenty][img->currentx];
133 rc = fread(pos, 1, count, img->f);
141 if (fread(&abyss, 1, 1, img->f) != 1)
149 lbx_errno = LBX_EEOF;
156 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
158 if (frame >= img->frames || frame < 0) {
159 lbx_errno = LBX_ERANGE;
163 if (!img->framedata) {
167 tmp = malloc(img->width * img->height);
173 img->framedata = malloc(img->height * sizeof *img->framedata);
174 if (!img->framedata) {
180 for (i = 0; i < img->height; i++) {
181 img->framedata[i] = tmp + i * img->width;
185 /* Start over if we are backtracking. */
186 if (img->currentframe > frame)
187 img->currentframe == -1;
189 /* We must have previous frame decoded to continue. */
190 if (frame > img->currentframe + 1) {
191 if (!lbximg_getframe(img, frame-1))
195 if (img->currentframe != frame) {
198 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
202 rc = _lbx_drawrow(first, img);
207 if (img->foff > img->offsets[frame+1]) {
208 lbx_errno = LBX_EFORMAT;
214 img->currentframe = frame;
215 return img->framedata;
218 void lbximg_close(struct lbx_image *img)
222 if (img->framedata) {
223 free(img->framedata[0]);
224 free(img->framedata);