MCDE dtpaint: 8-bit BMP (incl. RLE8) and ICO read (D4)

OpenBMP now dispatches on the file: 24-bit BMP keeps the proven path
(OpenBMP24), 8-bit palette BMP decodes uncompressed and RLE8 (count/value,
end-of-line, end-of-bitmap, delta, absolute runs with word padding, top-down
and bottom-up), and the first image of a .ico decodes 1/4/8/24/32 bpp
(BGRA with alpha 0 composited onto the background; PNG-compressed icons are
refused because there is no PNG decoder).  All three go through one RGB
buffer and SetImageRGB, which allocates distinct colours in one batch on
PseudoColor.  Measured under Xvfb: ImageMagick's RLE8 160x100 gradient opens
and shows the gradient; a 64x64 32-bpp ICO opens with its four colours;
24-bit -selftest still passes.
This commit is contained in:
lotherk committed 2026-11-22 07:18:21 +00:00
1 parent a196caf47b
commit fa06dc280a
1 file changed
+454 -1
+454 -1
View File
@@ -411,7 +411,7 @@ SaveBMP(const char *path)
}
static int
OpenBMP(const char *path)
OpenBMP24(const char *path)
{
FILE *f;
unsigned char hdr[54];
@@ -563,6 +563,459 @@ OpenBMP(const char *path)
return 0;
}
/* ---------------------------------------------------------- more formats */
static unsigned
LE16(const unsigned char *p)
{
return (unsigned) (p[0] | (p[1] << 8));
}
static unsigned long
LE32(const unsigned char *p)
{
return (unsigned long) p[0] | ((unsigned long) p[1] << 8) |
((unsigned long) p[2] << 16) | ((unsigned long) p[3] << 24);
}
/* install a top-down RGB buffer as the document (owner keeps the buffer) */
static int
SetImageRGB(const unsigned char *rgb, int w, int h)
{
Pixmap np, ns;
XImage *im;
int x, y;
unsigned long *keys = NULL, *kpix = NULL;
int nkeys = 0;
if (w < 1 || h < 1 || w > 4096 || h > 4096)
return -1;
np = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) w,
(unsigned) h, depth);
ns = XCreatePixmap(dpy, RootWindowOfScreen(scr), (unsigned) w,
(unsigned) h, depth);
if (np == None || ns == None) {
if (np != None)
XFreePixmap(dpy, np);
if (ns != None)
XFreePixmap(dpy, ns);
return -1;
}
if (pix != None)
XFreePixmap(dpy, pix);
if (snap != None)
XFreePixmap(dpy, snap);
pix = np;
snap = ns;
imgW = w;
imgH = h;
if (canvas != NULL)
XtVaSetValues(canvas, XmNwidth, imgW * zoom,
XmNheight, imgH * zoom, NULL);
XSetForeground(dpy, gc, bgPix);
XFillRectangle(dpy, pix, gc, 0, 0, (unsigned) w, (unsigned) h);
im = XCreateImage(dpy, vis, depth, ZPixmap, 0, NULL,
(unsigned) w, (unsigned) h, 32, 0);
if (im == NULL)
return -1;
im->data = calloc(1, (size_t) im->bytes_per_line * h);
if (im->data == NULL) {
XDestroyImage(im);
return -1;
}
if (truecolor) {
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
const unsigned char *p =
rgb + ((size_t) y * w + x) * 3;
XPutPixel(im, x, y, RGBToPixel(p[0], p[1], p[2]));
}
} else {
keys = malloc(sizeof(unsigned long) * (size_t) w * h);
if (keys != NULL) {
int i;
for (i = 0; i < w * h; i++)
keys[i] = ((unsigned long) rgb[i * 3] << 16) |
((unsigned long) rgb[i * 3 + 1] << 8) |
rgb[i * 3 + 2];
qsort(keys, (size_t) (w * h), sizeof(unsigned long),
CmpUL);
nkeys = 0;
for (i = 0; i < w * h; i++)
if (i == 0 || keys[i] != keys[i - 1])
keys[nkeys++] = keys[i];
kpix = malloc(sizeof(unsigned long) * (size_t) nkeys);
if (kpix != NULL) {
unsigned char *krgb =
malloc((size_t) nkeys * 3);
if (krgb != NULL) {
for (i = 0; i < nkeys; i++) {
krgb[i * 3] =
(unsigned char) (keys[i] >> 16);
krgb[i * 3 + 1] =
(unsigned char) (keys[i] >> 8);
krgb[i * 3 + 2] =
(unsigned char) keys[i];
}
BatchAlloc(krgb, nkeys, kpix);
free(krgb);
}
}
}
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
const unsigned char *p =
rgb + ((size_t) y * w + x) * 3;
unsigned long key =
((unsigned long) p[0] << 16) |
((unsigned long) p[1] << 8) | p[2];
unsigned long px = 0;
if (keys != NULL && kpix != NULL) {
unsigned long *hit = bsearch(&key, keys,
(size_t) nkeys, sizeof(unsigned long),
CmpUL);
if (hit != NULL)
px = kpix[hit - keys];
} else {
px = RGBToPixel(p[0], p[1], p[2]);
}
XPutPixel(im, x, y, px);
}
free(keys);
free(kpix);
}
XPutImage(dpy, pix, gc, im, 0, 0, 0, 0, (unsigned) w, (unsigned) h);
XDestroyImage(im);
return 0;
}
/* 8-bit palette BMP, uncompressed or RLE8 (the 24-bit path is OpenBMP24) */
static int
LoadBMP8(FILE *f)
{
unsigned char fh[14], ih[40], pal[256][3], *raw, *rgb, *idx;
unsigned long off, row, size;
long w, h, hs;
int bpp, comp, ncolors, i, y, x, topdown = 0;
if (fread(fh, 1, 14, f) != 14 || fh[0] != 'B' || fh[1] != 'M')
return -1;
if (fread(ih, 1, 40, f) != 40)
return -1;
if (LE32(ih) != 40 || LE16(ih + 12) != 1)
return -1;
bpp = (int) LE16(ih + 14);
comp = (int) LE32(ih + 16);
if (bpp != 8 || (comp != 0 && comp != 1))
return -1;
w = (long) LE32(ih + 4);
hs = (long) (int) LE32(ih + 8);
if (hs < 0) {
topdown = 1;
h = -hs;
} else {
h = hs;
}
if (w < 1 || h < 1 || w > 4096 || h > 4096)
return -1;
off = LE32(fh + 10);
ncolors = (int) LE32(ih + 32);
if (ncolors == 0)
ncolors = 256;
if (ncolors > 256)
ncolors = 256;
memset(pal, 0, sizeof pal);
if (fseek(f, 14 + 40, SEEK_SET) != 0)
return -1;
for (i = 0; i < ncolors; i++) {
unsigned char e[4];
if (fread(e, 1, 4, f) != 4)
return -1;
pal[i][0] = e[2];
pal[i][1] = e[1];
pal[i][2] = e[0];
}
idx = calloc((size_t) w * h, 1);
if (idx == NULL)
return -1;
if (comp == 0) {
row = (((unsigned long) w * 8 + 31) / 32) * 4;
raw = malloc((size_t) row * h);
if (raw == NULL) {
free(idx);
return -1;
}
if (fseek(f, (long) off, SEEK_SET) != 0 ||
fread(raw, 1, (size_t) row * h, f) != (size_t) row * h) {
free(raw);
free(idx);
return -1;
}
for (y = 0; y < h; y++) {
unsigned char *src =
raw + (topdown ? y : (h - 1 - y)) * row;
memcpy(idx + (size_t) y * w, src, (size_t) w);
}
free(raw);
} else {
/* RLE8: count/value pairs, 00 00 end of line, 00 01 end of
* bitmap, 00 02 dx dy delta, 00 n literals (padded to word) */
FILE *g = f;
long beg, end;
unsigned char *rl;
size_t p = 0;
if (fseek(g, 0, SEEK_END) != 0 ||
(end = ftell(g)) < 0 ||
fseek(g, (long) off, SEEK_SET) != 0) {
free(idx);
return -1;
}
beg = (long) off;
size = (unsigned long) (end - beg);
if (size > 64UL * 1024 * 1024) {
free(idx);
return -1;
}
rl = malloc((size_t) size);
if (rl == NULL || fread(rl, 1, (size_t) size, g) != (size_t) size) {
free(rl);
free(idx);
return -1;
}
x = 0;
y = topdown ? 0 : (int) h - 1;
while (p + 1 < size) {
int cnt = rl[p], val = rl[p + 1];
p += 2;
if (cnt > 0) {
int k;
for (k = 0; k < cnt; k++) {
if (x < w && y >= 0 && y < h)
idx[(size_t) y * w + x] =
(unsigned char) val;
x++;
}
} else if (val == 0) {
x = 0;
y += topdown ? 1 : -1;
} else if (val == 1) {
break;
} else if (val == 2) {
int dx, dy;
if (p + 1 >= size)
break;
dx = rl[p++];
dy = rl[p++];
x += dx;
y += topdown ? dy : -dy;
} else {
int k;
for (k = 0; k < val && p < size; k++) {
if (x < w && y >= 0 && y < h)
idx[(size_t) y * w + x] = rl[p];
x++;
p++;
}
if (val & 1)
p++;
}
}
free(rl);
}
rgb = malloc((size_t) w * h * 3);
if (rgb == NULL) {
free(idx);
return -1;
}
for (i = 0; i < w * h; i++) {
unsigned char c = idx[i];
if (c >= ncolors) {
rgb[i * 3] = rgb[i * 3 + 1] = rgb[i * 3 + 2] = 0;
} else {
rgb[i * 3] = pal[c][0];
rgb[i * 3 + 1] = pal[c][1];
rgb[i * 3 + 2] = pal[c][2];
}
}
free(idx);
i = SetImageRGB(rgb, (int) w, (int) h);
free(rgb);
return i;
}
/* the first image of a Windows .ico (1, 4, 8, 24 or 32 bpp; PNG icons are
* refused because the program has no PNG decoder) */
static int
LoadICO(FILE *f)
{
unsigned char dir[6], ent[16], ih[40], pal[256][3], *raw, *rgb;
unsigned long off, row;
long w, h, hs;
int bpp, ncolors, i, y, x;
unsigned br, bg, bb;
if (fread(dir, 1, 6, f) != 6 || LE16(dir) != 0 || LE16(dir + 2) != 1)
return -1;
if (LE16(dir + 4) < 1)
return -1;
if (fread(ent, 1, 16, f) != 16)
return -1;
off = LE32(ent + 12);
if (off < 22)
return -1;
if (fseek(f, (long) off, SEEK_SET) != 0)
return -1;
if (fread(ih, 1, 40, f) != 40)
return -1;
if (ih[0] == 0x89 && ih[1] == 'P' && ih[2] == 'N' && ih[3] == 'G')
return -1;
if (LE32(ih) != 40 || LE16(ih + 12) != 1 || LE32(ih + 16) != 0)
return -1;
w = (long) LE32(ih + 4);
hs = (long) (int) LE32(ih + 8);
if (hs <= 0 || (hs % 2) != 0)
return -1;
h = hs / 2;
if (w < 1 || h < 1 || w > 4096 || h > 4096)
return -1;
bpp = (int) LE16(ih + 14);
if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 24 && bpp != 32)
return -1;
ncolors = 0;
if (bpp <= 8) {
ncolors = (int) LE32(ih + 32);
if (ncolors == 0)
ncolors = 1 << bpp;
if (ncolors > 256)
ncolors = 256;
}
memset(pal, 0, sizeof pal);
for (i = 0; i < ncolors; i++) {
unsigned char e[4];
if (fread(e, 1, 4, f) != 4)
return -1;
pal[i][0] = e[2];
pal[i][1] = e[1];
pal[i][2] = e[0];
}
row = (((unsigned long) w * bpp + 31) / 32) * 4;
raw = malloc((size_t) row * h);
if (raw == NULL)
return -1;
if (fread(raw, 1, (size_t) row * h, f) != (size_t) row * h) {
free(raw);
return -1;
}
rgb = malloc((size_t) w * h * 3);
if (rgb == NULL) {
free(raw);
return -1;
}
PixelToRGB(bgPix, &br, &bg, &bb);
for (y = 0; y < h; y++) {
unsigned char *src = raw + (h - 1 - y) * row;
for (x = 0; x < w; x++) {
unsigned char *d = rgb + ((size_t) y * w + x) * 3;
if (bpp == 32) {
unsigned a = src[x * 4 + 3];
d[0] = src[x * 4 + 2];
d[1] = src[x * 4 + 1];
d[2] = src[x * 4];
if (a == 0) {
d[0] = (unsigned char) br;
d[1] = (unsigned char) bg;
d[2] = (unsigned char) bb;
}
} else if (bpp == 24) {
d[0] = src[x * 3 + 2];
d[1] = src[x * 3 + 1];
d[2] = src[x * 3];
} else {
int idx;
if (bpp == 8)
idx = src[x];
else if (bpp == 4)
idx = (x & 1) ? (src[x / 2] & 15) :
(src[x / 2] >> 4);
else
idx = (src[x / 8] >> (7 - (x & 7))) & 1;
if (idx >= ncolors) {
d[0] = d[1] = d[2] = 0;
} else {
d[0] = pal[idx][0];
d[1] = pal[idx][1];
d[2] = pal[idx][2];
}
}
}
}
free(raw);
i = SetImageRGB(rgb, (int) w, (int) h);
free(rgb);
return i;
}
/* Open a BMP (24 or 8 bit) or the first image of an ICO. */
static int
OpenBMP(const char *path)
{
FILE *f;
unsigned char m[6], hdr[54];
int bpp;
if ((f = fopen(path, "rb")) == NULL)
return -1;
if (fread(m, 1, 6, f) != 6) {
fclose(f);
return -1;
}
if (m[0] == 'B' && m[1] == 'M') {
fseek(f, 0, SEEK_SET);
if (fread(hdr, 1, 54, f) != 54 || LE32(hdr + 14) != 40) {
fclose(f);
return -1;
}
bpp = (int) LE16(hdr + 28);
if (bpp == 24) {
fclose(f);
return OpenBMP24(path);
}
if (bpp == 8) {
fseek(f, 0, SEEK_SET);
bpp = LoadBMP8(f);
fclose(f);
return bpp;
}
} else if (m[0] == 0 && m[1] == 0 && m[2] == 1 && m[3] == 0) {
fseek(f, 0, SEEK_SET);
bpp = LoadICO(f);
fclose(f);
return bpp;
}
fclose(f);
return -1;
}
/* --------------------------------------------------------------- drawing */
static void