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;
36 struct lbx_image *lbximg_fopen(FILE *f)
38 struct lbx_image tmp = {.f = f}, *new = NULL;
41 if (fread(&tmp.width, sizeof tmp.width, 1, f) != 1) goto readerr;
42 if (fread(&tmp.height, sizeof tmp.height, 1, f) != 1) goto readerr;
43 if (fread(&tmp.wtf1, sizeof tmp.wtf1, 1, f) != 1) goto readerr;
44 if (fread(&tmp.frames, sizeof tmp.frames, 1, f) != 1) goto readerr;
45 if (fread(&tmp.leadin, sizeof tmp.leadin, 1, f) != 1) goto readerr;
46 if (fread(&tmp.flags, sizeof tmp.flags, 1, f) != 1) goto readerr;
48 tmp.width = letohs(tmp.width); tmp.foff += sizeof tmp.width;
49 tmp.height = letohs(tmp.height); tmp.foff += sizeof tmp.height;
50 tmp.wtf1 = letohs(tmp.wtf1); tmp.foff += sizeof tmp.wtf1;
51 tmp.frames = letohs(tmp.frames); tmp.foff += sizeof tmp.frames;
52 tmp.leadin = letohs(tmp.leadin); tmp.foff += sizeof tmp.leadin;
53 tmp.flags = letohs(tmp.flags); tmp.foff += sizeof tmp.flags;
55 /* Format constraints. */
56 if (tmp.frames <= tmp.leadin) {
57 lbx_errno = LBX_EFORMAT;
62 * DEBUG ONLY. These assertions exist to catch otherwise valid image
63 * files which differ from what I believe to be true of all LBX images.
64 * If we never find any exceptions, we can replace the assertions with
67 _lbx_assert(tmp.wtf1 == 0);
68 _lbx_assert(!(tmp.flags & ~(FLAG_PALETTE|FLAG_OVERWRITE|FLAG_LOOPING)));
70 new = malloc(sizeof *new + (tmp.frames+1) * sizeof *new->offsets);
77 new->currentframe = -1;
79 rc = fread(new->offsets, sizeof *new->offsets, new->frames+1, f);
80 if (rc < new->frames+1)
82 new->foff += sizeof *new->offsets * (new->frames+1);
96 static int _lbx_drawrow(int first, struct lbx_image *img)
98 uint16_t type, yval, count, xval;
103 assert(img->framedata);
105 if (fread(&type, sizeof type, 1, img->f) != 1) goto readerr;
106 type = letohs(type); img->foff += sizeof type;
115 if (fread(&yval, sizeof yval, 1, img->f) != 1) goto readerr;
116 yval = letohs(yval); img->foff += sizeof yval;
119 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
120 count = letohs(count); img->foff += sizeof count;
121 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
122 xval = letohs(xval); img->foff += sizeof xval;
126 /* Ensure that the row fits in the image. */
127 if (img->height - img->currenty <= yval || xval >= img->width) {
128 lbx_errno = LBX_EFORMAT;
132 img->currenty += yval;
133 img->currentx = xval;
135 if (fread(&xval, sizeof xval, 1, img->f) != 1) goto readerr;
136 xval = letohs(xval); img->foff += sizeof xval;
138 if (img->width - img->currentx <= xval) {
139 lbx_errno = LBX_EFORMAT;
142 img->currentx += xval;
148 if (count > img->width - img->currentx) {
149 lbx_errno = LBX_EFORMAT;
153 pos = &img->framedata[img->currenty][img->currentx];
154 rc = fread(pos, 1, count, img->f);
162 if (fread(&abyss, 1, 1, img->f) != 1)
170 lbx_errno = LBX_EEOF;
177 unsigned char **lbximg_getframe(struct lbx_image *img, int frame)
179 if (frame >= img->frames || frame < 0) {
180 lbx_errno = LBX_ERANGE;
184 if (!img->framedata) {
188 tmp = malloc(img->width * img->height);
194 img->framedata = malloc(img->height * sizeof *img->framedata);
195 if (!img->framedata) {
201 for (i = 0; i < img->height; i++) {
202 img->framedata[i] = tmp + i * img->width;
206 /* Start over if we are backtracking. */
207 if (img->currentframe > frame)
208 img->currentframe == -1;
210 if (img->flags & FLAG_OVERWRITE) {
211 /* Clear the slate. */
212 memset(img->framedata[0], 0, img->width * img->height);
214 /* We must have previous frame decoded to continue. */
215 if (frame > img->currentframe + 1) {
216 if (!lbximg_getframe(img, frame-1))
221 if (img->currentframe != frame) {
224 if (_lbx_fseek(img->f, &img->foff, img->offsets[frame]) == -1)
228 rc = _lbx_drawrow(first, img);
233 if (!rc && img->foff > img->offsets[frame+1]) {
234 lbx_errno = LBX_EFORMAT;
240 img->currentframe = frame;
241 return img->framedata;
244 int lbximg_loadpalette(FILE *f, struct lbx_colour palette[static 256])
249 for (i = 0; i < 256; i++) {
250 if (fread(entry, sizeof entry, 1, f) != 1) {
251 lbx_errno = (feof(f)) ? LBX_EEOF : -errno;
256 lbx_errno = LBX_EFORMAT;
260 palette[i] = (struct lbx_colour){
261 .red = entry[1] << 2,
262 .green = entry[2] << 2,
263 .blue = entry[3] << 2,
271 lbximg_getpalette(struct lbx_image *img, struct lbx_colour palette[static 256])
273 size_t hdrlen = 6*(sizeof img->wtf1)+(img->frames+1)*(sizeof *img->offsets);
277 uint16_t start, count;
280 /* Do nothing if the image doesn't have embedded palette data. */
281 if (!(img->flags & FLAG_PALETTE))
284 /* Palette data is located right after the header. */
285 if (_lbx_fseek(img->f, &img->foff, hdrlen) == -1)
289 if (fread(&start, sizeof start, 1, img->f) != 1) goto readerr;
290 if (fread(&count, sizeof count, 1, img->f) != 1) goto readerr;
291 start = letohs(start); img->foff += sizeof start;
292 count = letohs(count); img->foff += sizeof count;
294 if (start + count > 256) {
295 lbx_errno = LBX_EFORMAT;
299 if (hdrlen + 2*sizeof start + count*sizeof entry > img->offsets[0]) {
300 lbx_errno = LBX_EFORMAT;
304 for (i = 0; i < count; i++) {
305 rc = fread(entry, 1, sizeof entry, img->f);
308 if (rc < sizeof entry) {
313 lbx_errno = LBX_EFORMAT;
317 palette[start + i] = (struct lbx_colour){
318 .red = entry[1] << 2,
319 .green = entry[2] << 2,
320 .blue = entry[3] << 2,
326 lbx_errno = feof(img->f) ? LBX_EEOF : -errno;
330 void lbximg_getinfo(struct lbx_image *img, struct lbx_imginfo *info)
332 *info = (struct lbx_imginfo) {
334 .height = img->height,
335 .nframes = img->frames,
336 .haspalette = (_Bool)(img->flags & FLAG_PALETTE),
339 /* There seems to be two ways of specifying that an image loops. */
340 if (img->flags & FLAG_LOOPING) {
343 } else if (img->leadin != img->frames - 1) {
344 info->loopstart = img->leadin;
349 void lbximg_close(struct lbx_image *img)
353 if (img->framedata) {
354 free(img->framedata[0]);
355 free(img->framedata);