LBM loader: Cleanups

This commit is contained in:
Kim Woelders 2021-09-28 18:54:51 +02:00
parent ff79901a07
commit 64bf73d851
1 changed files with 87 additions and 80 deletions

View File

@ -13,15 +13,17 @@
#include "loader_common.h" #include "loader_common.h"
#define L2RLONG(a) ((((long)((a)[0]) & 0xff) << 24) + (((long)((a)[1]) & 0xff) << 16) + (((long)((a)[2]) & 0xff) << 8) + ((long)((a)[3]) & 0xff)) #define DBG_PFX "LDR-lbm"
#define L2RWORD(a) ((((long)((a)[0]) & 0xff) << 8) + ((long)((a)[1]) & 0xff))
typedef struct CHUNK { #define L2RLONG(a) ((((int)((a)[0]) & 0xff) << 24) + (((int)((a)[1]) & 0xff) << 16) + (((int)((a)[2]) & 0xff) << 8) + ((int)((a)[3]) & 0xff))
long size; #define L2RWORD(a) ((((int)((a)[0]) & 0xff) << 8) + ((int)((a)[1]) & 0xff))
typedef struct {
int size;
unsigned char *data; unsigned char *data;
} CHUNK; } CHUNK;
typedef struct ILBM { typedef struct {
CHUNK bmhd; CHUNK bmhd;
CHUNK camg; CHUNK camg;
CHUNK cmap; CHUNK cmap;
@ -73,84 +75,89 @@ loadchunks(FILE * f, ILBM * ilbm, int full)
{ {
CHUNK *c; CHUNK *c;
size_t s; size_t s;
long formsize, pos, z; long pos;
int formsize, z;
int ok, seek; int ok, seek;
char buf[12]; char buf[12];
ok = 0; ok = 0;
s = fread(buf, 1, 12, f); s = fread(buf, 1, 12, f);
if (s == 12 && !memcmp(buf, "FORM", 4) && !memcmp(buf + 8, "ILBM", 4)) if (s != 12)
return ok;
if (memcmp(buf, "FORM", 4) != 0 || memcmp(buf + 8, "ILBM", 4) != 0)
return ok;
formsize = L2RLONG(buf + 4);
D("%s: %.4s %.4s formsize=%d\n", __func__, buf, buf + 8, formsize);
while (1)
{ {
memset(ilbm, 0, sizeof(*ilbm)); pos = ftell(f);
formsize = L2RLONG(buf + 4); if (pos < 0 || pos >= formsize + 8)
break; /* Error or FORM data is finished. */
seek = 1;
while (1) s = fread(buf, 1, 8, f);
if (s != 8)
break; /* Error or short file. */
z = L2RLONG(buf + 4);
if (z < 0)
break; /* Corrupt file. */
D("%s: %.4s %d\n", __func__, buf, z);
c = NULL;
if (!memcmp(buf, "BMHD", 4))
c = &(ilbm->bmhd);
else if (full)
{ {
pos = ftell(f); if (!memcmp(buf, "CAMG", 4))
if (pos < 0 || pos >= formsize + 8) c = &(ilbm->camg);
break; /* Error or FORM data is finished. */ else if (!memcmp(buf, "CMAP", 4))
seek = 1; c = &(ilbm->cmap);
else if (!memcmp(buf, "CTBL", 4))
s = fread(buf, 1, 8, f); c = &(ilbm->ctbl);
if (s != 8) else if (!memcmp(buf, "SHAM", 4))
break; /* Error or short file. */ c = &(ilbm->sham);
else if (!memcmp(buf, "BODY", 4))
z = L2RLONG(buf + 4); c = &(ilbm->body);
if (z < 0)
break; /* Corrupt file. */
c = NULL;
if (!memcmp(buf, "BMHD", 4))
c = &(ilbm->bmhd);
else if (full)
{
if (!memcmp(buf, "CAMG", 4))
c = &(ilbm->camg);
else if (!memcmp(buf, "CMAP", 4))
c = &(ilbm->cmap);
else if (!memcmp(buf, "CTBL", 4))
c = &(ilbm->ctbl);
else if (!memcmp(buf, "SHAM", 4))
c = &(ilbm->sham);
else if (!memcmp(buf, "BODY", 4))
c = &(ilbm->body);
}
if (c && !c->data)
{
c->size = z;
c->data = malloc(c->size);
if (!c->data)
break; /* Out of memory. */
s = fread(c->data, 1, c->size, f);
if (s != (size_t)c->size)
break; /* Error or short file. */
seek = 0;
if (!full)
{ /* Only BMHD required. */
ok = 1;
break;
}
}
if (pos + 8 + z >= formsize + 8)
break; /* This was last chunk. */
if (seek && fseek(f, z, SEEK_CUR) != 0)
break;
} }
/* File may end strangely, especially if body size is uneven, but it's if (c && !c->data)
* ok if we have the chunks we want. !full check is already done. */ {
if (ilbm->bmhd.data && ilbm->body.data) c->size = z;
ok = 1; c->data = malloc(c->size);
if (!ok) if (!c->data)
freeilbm(ilbm); break; /* Out of memory. */
s = fread(c->data, 1, c->size, f);
if (s != (size_t)c->size)
break; /* Error or short file. */
seek = 0;
if (!full)
{ /* Only BMHD required. */
ok = 1;
break;
}
}
if (pos + 8 + z >= formsize + 8)
break; /* This was last chunk. */
if (seek && fseek(f, z, SEEK_CUR) != 0)
break;
} }
/* File may end strangely, especially if body size is uneven, but it's
* ok if we have the chunks we want. !full check is already done. */
if (ilbm->bmhd.data && ilbm->body.data)
ok = 1;
return ok; return ok;
} }
@ -290,9 +297,10 @@ scalecmap(ILBM * ilbm)
static void static void
deplane(DATA32 * row, int w, ILBM * ilbm, unsigned char *plane[]) deplane(DATA32 * row, int w, ILBM * ilbm, unsigned char *plane[])
{ {
unsigned long l; unsigned int l, r, g, b, a;
int i, o, x; int i, o, x;
unsigned char bit, r, g, b, a, v, h, *pal; unsigned char bit, v, h;
const unsigned char *pal;
pal = NULL; pal = NULL;
if (ilbm->sham.data && ilbm->sham.size >= 2 + (ilbm->row + 1) * 2 * 16) if (ilbm->sham.data && ilbm->sham.size >= 2 + (ilbm->row + 1) * 2 * 16)
@ -428,9 +436,7 @@ deplane(DATA32 * row, int w, ILBM * ilbm, unsigned char *plane[])
a = 0x00; a = 0x00;
} }
row[x] = row[x] = PIXEL_ARGB(a, r, g, b);
((unsigned long)a << 24) | ((unsigned long)r << 16) |
((unsigned long)g << 8) | (unsigned long)b;
bit = bit >> 1; bit = bit >> 1;
if (bit == 0) if (bit == 0)
@ -456,19 +462,20 @@ load2(ImlibImage * im, int load_data)
unsigned char *plane[40]; unsigned char *plane[40];
ILBM ilbm; ILBM ilbm;
rc = LOAD_FAIL;
plane[0] = NULL;
memset(&ilbm, 0, sizeof(ilbm));
/*---------- /*----------
* Load the chunk(s) we're interested in. If load_data is not true, then we only * Load the chunk(s) we're interested in. If load_data is not true, then we only
* want the image size and format. * want the image size and format.
*----------*/ *----------*/
rc = loadchunks(im->fp, &ilbm, load_data); if (!loadchunks(im->fp, &ilbm, load_data))
if (rc == 0) goto quit;
return LOAD_FAIL;
/*---------- /*----------
* Use and check header. * Use and check header.
*----------*/ *----------*/
rc = LOAD_FAIL;
plane[0] = NULL;
if (ilbm.bmhd.size < 20) if (ilbm.bmhd.size < 20)
goto quit; goto quit;