10 #include "byteorder.h"
16 uint16_t width, height;
18 uint16_t offs, frames;
25 int currentx, currenty;
26 unsigned char **framedata;
31 struct lbx_image *lbximg_fopen(FILE *f)
33 struct lbx_image tmp = {.f = f}, *new = NULL;
35 if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
36 if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
37 if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr;
38 if (fread(&tmp.offs, sizeof tmp.offs, 1, f) != 1) goto readerr;
39 if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr;
40 if (fread(&tmp.pal, sizeof tmp.pal, 1, f) != 1) goto readerr;
42 tmp.width = letohs(tmp.width); tmp.foff += sizeof tmp.width;
43 tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
44 tmp.wtf1 = letohs(tmp.wtf1); tmp.foff += sizeof tmp.wtf1;
45 tmp.offs = letohs(tmp.offs); tmp.foff += sizeof tmp.offs;
46 tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
47 tmp.pal = letohs(tmp.pal); tmp.foff += sizeof tmp.pal;
49 /* For some reason, the format seems to need this. */
53 /* Format constraints. */
54 if (tmp.offs <= tmp.frames) {
55 lbx_errno = LBX_EFORMAT;
60 * DEBUG ONLY. These assertions exist to catch otherwise valid image
61 * files which differ from what I believe to be true of all LBX images.
62 * If we never find any exceptions, we can replace the assertions with
65 _lbx_assert(tmp.wtf1 == 0);
66 _lbx_assert(tmp.offs == tmp.frames + 1);
67 _lbx_assert(tmp.pal == 0 || tmp.pal == 0x1000);
69 new = malloc(sizeof *new + tmp.offs * sizeof *new->offsets);
76 new->currentframe = -1;
78 if (fread(new->offsets, sizeof *new->offsets, new->offs, f)!= new->offs)
80 new->foff += sizeof *new->offsets * new->offs;
94 static int _lbx_drawrow(int first, struct lbx_image *img)
96 uint16_t type, yval, count, xval;
101 assert(img->framedata);
103 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
104 type = letohs(type); img->foff += sizeof type;
113 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
114 yval = letohs(yval); img->foff += sizeof yval;
117 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
118 count = letohs(count); img->foff += sizeof count;
119 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
120 xval = letohs(xval); img->foff += sizeof xval;
124 /* Ensure that the row fits in the image. */
125 if (img->height - img->currenty <= yval || xval >= img->width) {
126 lbx_errno = LBX_EFORMAT;
130 img->currenty += yval;
131 img->currentx = xval;
133 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
134 xval = letohs(xval); img->foff += sizeof xval;
136 if (img->width - img->currentx <= xval) {
137 lbx_errno = LBX_EFORMAT;
140 img->currentx += xval;
146 if (count > img->width - img->currentx) {
147 lbx_errno = LBX_EFORMAT;
151 pos = &img->framedata[img->currenty][img->currentx];
152 rc = fread(pos, 1, count, img->f);
160 if (fread(&abyss, 1, 1, img->f) != 1)
168 lbx_errno = LBX_EEOF;
175 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
177 if (frame >= img->frames || frame < 0) {
178 lbx_errno = LBX_ERANGE;
182 if (!img->framedata) {
186 tmp = malloc(img->width * img->height);
192 img->framedata = malloc(img->height * sizeof *img->framedata);
193 if (!img->framedata) {
199 for (i = 0; i < img->height; i++) {
200 img->framedata[i] = tmp + i * img->width;
204 /* Start over if we are backtracking. */
205 if (img->currentframe > frame)
206 img->currentframe == -1;
208 /* We must have previous frame decoded to continue. */
209 if (frame > img->currentframe + 1) {
210 if (!lbximg_getframe(img, frame-1))
214 if (img->currentframe != frame) {
217 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
221 rc = _lbx_drawrow(first, img);
226 if (!rc && img->foff > img->offsets[frame+1]) {
227 lbx_errno = LBX_EFORMAT;
233 img->currentframe = frame;
234 return img->framedata;
237 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
242 for (i = 0; i < 256; i++) {
243 if (fread(entry, sizeof entry, 1, f) != 1) {
244 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
249 lbx_errno = LBX_EFORMAT;
253 palette[i] = (struct lbx_colour){
254 .red = entry[1] << 2,
255 .green = entry[2] << 2,
256 .blue = entry[3] << 2,
264 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
266 size_t hdrlen = 6*(sizeof img->pal)+(img->offs)*(sizeof *img->offsets);
270 uint16_t start, count;
273 /* Do nothing if the image doesn't have embedded palette data. */
277 /* Palette data is located right after the header. */
278 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
282 if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
283 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
284 start = letohs(start); img->foff += sizeof start;
285 count = letohs(count); img->foff += sizeof count;
287 if (start + count > 256) {
288 lbx_errno = LBX_EFORMAT;
292 if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
293 lbx_errno = LBX_EFORMAT;
297 for (i = 0; i < count; i++) {
298 rc = fread(entry, 1, sizeof entry, img->f);
301 if (rc < sizeof entry) {
306 lbx_errno = LBX_EFORMAT;
310 palette[start + i] = (struct lbx_colour){
311 .red = entry[1] << 2,
312 .green = entry[2] << 2,
313 .blue = entry[3] << 2,
319 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
323 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
325 *info = (struct lbx_imginfo) {
327 .height = img->height,
328 .nframes = img->frames,
332 void lbximg_close(struct lbx_image *img)
336 if (img->framedata) {
337 free(img->framedata[0]);
338 free(img->framedata);