11 #include "byteorder.h"
16 #define FLAG_OVERWRITE 0x0400 /* Draw each frame on a clean slate (unsure). */
17 #define FLAG_PALETTE 0x1000 /* Image contains embedded palette. */
18 #define FLAG_LOOPING 0x2000 /* Loop over all frames in the image (unsure). */
21 uint16_t width, height;
23 uint16_t frames, leadin;
30 int currentx, currenty;
31 unsigned char **framedata;
37 struct lbx_image *lbximg_fopen(FILE *f)
39 struct lbx_image tmp = {.f = f}, *new = NULL;
42 if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
43 if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
44 if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr;
45 if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr;
46 if (fread(&tmp.leadin, sizeof tmp.leadin, 1, f) != 1) goto readerr;
47 if (fread(&tmp.flags, sizeof tmp.flags, 1, f) != 1) goto readerr;
49 tmp.width = letohs(tmp.width); tmp.foff += sizeof tmp.width;
50 tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
51 tmp.wtf1 = letohs(tmp.wtf1); tmp.foff += sizeof tmp.wtf1;
52 tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
53 tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
54 tmp.flags = letohs(tmp.flags); tmp.foff += sizeof tmp.flags;
56 /* Format constraints. */
57 if (tmp.frames <= tmp.leadin) {
58 lbx_errno = LBX_EFORMAT;
63 * DEBUG ONLY. These assertions exist to catch otherwise valid image
64 * files which differ from what I believe to be true of all LBX images.
65 * If we never find any exceptions, we can replace the assertions with
68 _lbx_assert(tmp.wtf1 == 0);
69 _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
71 new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
78 new->currentframe = -1;
80 rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
81 if (rc < new->frames+1)
83 new->foff += sizeof *new->offsets * (new->frames+1);
97 static int _lbx_drawrow(int first, struct lbx_image *img)
99 uint16_t type, yval, count, xval;
104 assert(img->framedata);
107 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
108 type = letohs(type); img->foff += sizeof type;
117 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
118 yval = letohs(yval); img->foff += sizeof yval;
121 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
122 count = letohs(count); img->foff += sizeof count;
123 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
124 xval = letohs(xval); img->foff += sizeof xval;
128 /* Ensure that the row fits in the image. */
129 if (img->height - img->currenty <= yval || xval >= img->width) {
130 lbx_errno = LBX_EFORMAT;
134 img->currenty += yval;
135 img->currentx = xval;
137 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
138 xval = letohs(xval); img->foff += sizeof xval;
140 if (img->width - img->currentx <= xval) {
141 lbx_errno = LBX_EFORMAT;
144 img->currentx += xval;
150 if (count > img->width - img->currentx) {
151 lbx_errno = LBX_EFORMAT;
155 memset(&img->mask[img->currenty][img->currentx], 1, count);
157 pos = &img->framedata[img->currenty][img->currentx];
158 rc = fread(pos, 1, count, img->f);
166 if (fread(&abyss, 1, 1, img->f) != 1)
174 lbx_errno = LBX_EEOF;
181 static unsigned char **allocframebuffer(size_t width, size_t height)
183 unsigned char **new, *tmp;
186 tmp = calloc(height, width);
192 new = malloc(height * sizeof *new);
199 for (i = 0; i < height; i++) {
200 new[i] = tmp + i * width;
206 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
208 if (frame >= img->frames || frame < 0) {
209 lbx_errno = LBX_ERANGE;
213 if (!img->framedata) {
214 img->framedata = allocframebuffer(img->width, img->height);
220 img->mask = allocframebuffer(img->width, img->height);
225 /* Start over if we are backtracking. */
226 if (img->currentframe > frame)
227 img->currentframe == -1;
229 if (img->flags & FLAG_OVERWRITE) {
230 /* Clear the slate. */
231 memset(img->framedata[0], 0, img->width * img->height);
232 memset(img->mask[0], 0, img->width * img->height);
234 /* We must have previous frame decoded to continue. */
235 if (frame > img->currentframe + 1) {
236 if (!lbximg_getframe(img, frame-1))
241 if (img->currentframe != frame) {
244 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
248 rc = _lbx_drawrow(first, img);
253 if (!rc && img->foff > img->offsets[frame+1]) {
254 lbx_errno = LBX_EFORMAT;
260 img->currentframe = frame;
261 return img->framedata;
264 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
269 for (i = 0; i < 256; i++) {
270 if (fread(entry, sizeof entry, 1, f) != 1) {
271 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
276 lbx_errno = LBX_EFORMAT;
280 palette[i] = (struct lbx_colour){
281 .red = entry[1] << 2,
282 .green = entry[2] << 2,
283 .blue = entry[3] << 2,
291 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
293 size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
297 uint16_t start, count;
300 /* Do nothing if the image doesn't have embedded palette data. */
301 if (!(img->flags & FLAG_PALETTE))
304 /* Palette data is located right after the header. */
305 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
309 if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
310 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
311 start = letohs(start); img->foff += sizeof start;
312 count = letohs(count); img->foff += sizeof count;
314 if (start + count > 256) {
315 lbx_errno = LBX_EFORMAT;
319 if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
320 lbx_errno = LBX_EFORMAT;
324 for (i = 0; i < count; i++) {
325 rc = fread(entry, 1, sizeof entry, img->f);
328 if (rc < sizeof entry) {
333 lbx_errno = LBX_EFORMAT;
337 palette[start + i] = (struct lbx_colour){
338 .red = entry[1] << 2,
339 .green = entry[2] << 2,
340 .blue = entry[3] << 2,
346 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
350 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
352 *info = (struct lbx_imginfo) {
354 .height = img->height,
355 .nframes = img->frames,
356 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
359 /* There seems to be two ways of specifying that an image loops. */
360 if (img->flags & FLAG_LOOPING) {
363 } else if (img->leadin != img->frames - 1) {
364 info->loopstart = img->leadin;
369 unsigned char **lbximg_getmask(struct lbx_image *img)
374 void lbximg_close(struct lbx_image *img)
378 if (img->framedata) {
379 free(img->framedata[0]);
380 free(img->framedata);