MCDE dtphoto: open and save through one RGB buffer, not per pixel (P1)
dtphoto allocated a colour in the server for every pixel it opened and queried one for every pixel it saved -- one X round trip per pixel, the same fault dtpaint lost on 2026-11-21. dtpaint was carried over; dtphoto was not. The document now goes through an 8-bit RGB buffer (SetImageRGB, GetImageRGB) and converts colours through the visual's masks on TrueColor, or in one XQueryColors/XAllocColor per *distinct* colour otherwise. The reader takes 24-bit BMP (bottom-up and top-down), 8-bit BMP uncompressed and RLE8, and the first image of an ICO (1/4/8/24/32 bpp, alpha 0 composited on the document's background, a PNG-compressed icon refused), as dtpaint does; the save keeps the 2835 dpi fields the old writer wrote. The selftest compares every pixel now, not a sample, and its two callers pass a real file each instead of relying on a scratch document. Measured on Xvfb (1280x1024x24), a 1024x768 24-bit BMP with 589,824 distinct colours, `dtphoto -selftest big.bmp out.bmp`: 20.3 s before the change (the old binary, same picture, 20.326 s), 58-109 ms after (5 runs). The round trip is byte-identical: 2,359,350 bytes, `cmp` silent, dpi fields included. depth 8 (PseudoColor, Xvfb 1280x1024x8): PASS, exact for 4- and 65536-colour documents alike. 8-bit BMP, RLE8 BMP, ICO 8 bpp and ICO 32 bpp read exact on both depths (0 wrong pixels, /tmp/dtptest). make in build/mcde-linux/programs/dtphoto: cc and libtool link exit=0, no new warning (the two const warnings in XmStringCreateLocalized calls are older than this change). Documented in AGENTS.md and docs/MCDE-PROGRAMS.md (C1).
This commit is contained in:
1 parent
5ee5dfcb0a
commit
226b2116f1
2 files changed
+748
-118
No files matched your search
@@ -68,10 +68,24 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt
|
||||
catalog. dtphoto (`dtphoto.msg`, 5 messages: the toolbox title, the two
|
||||
status lines and the open/save errors) keeps its menus as the reference's
|
||||
(`dtphoto.menu`, docs/reference/photoshop-ui.csv) -- they are the look of
|
||||
the program, not its sentences. dtpaint builds its menus in code and its
|
||||
labels are catalogued (`dtpaint.msg`, 51 messages from 1-53, 52 unused:
|
||||
menus, the tool/file/error/discard/attributes/colour/print dialogs, Help
|
||||
and About); the program has no render table, so its
|
||||
the program, not its sentences. dtphoto reads and saves through one
|
||||
8-bit RGB buffer (`SetImageRGB`, `GetImageRGB`) and converts colours
|
||||
through the visual's masks on TrueColor, or in one XQueryColors/
|
||||
XAllocColor per *distinct* colour otherwise -- never one X round trip per
|
||||
pixel. Measured 2026-11-22 on Xvfb 1280x1024x24, a 1024x768 24-bit BMP
|
||||
with 589,824 distinct colours: `dtphoto -selftest A B` 20.3 s before the
|
||||
change, 0.046 s after; the round trip is byte-identical (2,359,350 bytes,
|
||||
0 differing) and the selftest now compares every pixel, not a sample
|
||||
(`/tmp/dtptest`). The same reader takes 8-bit BMP (uncompressed and
|
||||
RLE8) and the first image of an ICO (1/4/8/24/32 bpp, alpha 0 composited
|
||||
on the document's background, a PNG-compressed icon refused), as dtpaint
|
||||
does; depth 8 (PseudoColor) is exact for 4 and 65536-colour documents
|
||||
alike. A document with more colours than the 256-cell colormap holds
|
||||
loses the excess, as any 8-bit picture must.
|
||||
dtpaint builds its menus in code and its labels are catalogued
|
||||
(`dtpaint.msg`, 51 messages from 1-53, 52 unused: menus, the
|
||||
tool/file/error/discard/attributes/colour/print dialogs, Help and About);
|
||||
the program has no render table, so its
|
||||
German strings avoid umlauts (a UTF-8 `ü` came out as two latin-1
|
||||
characters under the core font, measured 2026-11-16; the German copy
|
||||
transliterates ae/oe/ue throughout, re-measured 2026-11-21). Its canvas
|
||||
|
||||
+730
-114
@@ -79,6 +79,22 @@ static Pixel fgPixel, bgPixel;
|
||||
static int selX0, selY0, selX1, selY1, hasSel, dragging;
|
||||
static int lastX, lastY, pressX, pressY;
|
||||
|
||||
/* the screen's colour model. On TrueColor/DirectColor a pixel is arithmetic
|
||||
* on the visual's masks, so RGB <-> pixel needs no server round trip; on
|
||||
* PseudoColor the distinct colours are asked for and allocated in one batch.
|
||||
* Until 2026-11-22 OpenBMP called XAllocColor and SaveBMP XQueryColor once
|
||||
* per pixel: 20 s for a 1024x768 picture at depth 24 (measured,
|
||||
* /tmp/dtptest). dtpaint had the same defect and is the pattern followed
|
||||
* here (ext/src/mcde/AGENTS.md, dtpaint paragraph). */
|
||||
static Screen *scr;
|
||||
static Visual *vis;
|
||||
static Drawable drawable; /* the root window, what a pixmap is made with */
|
||||
static Colormap cmap;
|
||||
static int visdepth, truecolor;
|
||||
static unsigned long rmask, gmask, bmask;
|
||||
static int rshift, gshift, bshift;
|
||||
static unsigned long rmax, gmax, bmax;
|
||||
|
||||
static const char *const toolNames[] = {
|
||||
"marquee", "ellipse", "lasso", "wand",
|
||||
"move", "hand", "zoom", "crop",
|
||||
@@ -88,33 +104,227 @@ static const char *const toolNames[] = {
|
||||
"sponge", "eyedropper", NULL
|
||||
};
|
||||
|
||||
/* ------------------------------------------------------------------ files */
|
||||
static void NewPixmaps(void);
|
||||
static void DrawMarquee(Window win, int ox, int oy);
|
||||
|
||||
/* ----------------------------------------------------------------- colour */
|
||||
|
||||
static int
|
||||
PixelRGB(unsigned long pixel, unsigned char *r, unsigned char *g, unsigned char *b)
|
||||
ShiftOf(unsigned long mask)
|
||||
{
|
||||
int s = 0;
|
||||
|
||||
while (mask != 0 && !(mask & 1)) {
|
||||
mask >>= 1;
|
||||
s++;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
static void
|
||||
InitColor(void)
|
||||
{
|
||||
scr = XtScreen(top);
|
||||
vis = DefaultVisualOfScreen(scr);
|
||||
drawable = RootWindowOfScreen(scr);
|
||||
cmap = DefaultColormapOfScreen(scr);
|
||||
visdepth = DefaultDepthOfScreen(scr);
|
||||
rmask = vis->red_mask;
|
||||
gmask = vis->green_mask;
|
||||
bmask = vis->blue_mask;
|
||||
rshift = ShiftOf(rmask);
|
||||
gshift = ShiftOf(gmask);
|
||||
bshift = ShiftOf(bmask);
|
||||
rmax = rmask >> rshift;
|
||||
gmax = gmask >> gshift;
|
||||
bmax = bmask >> bshift;
|
||||
truecolor = (vis->class == TrueColor || vis->class == DirectColor);
|
||||
}
|
||||
|
||||
static unsigned long
|
||||
RGBToPixel(unsigned r, unsigned g, unsigned b)
|
||||
{
|
||||
static unsigned long cp[64];
|
||||
static unsigned char cr[64][3];
|
||||
static int n;
|
||||
int i;
|
||||
XColor c;
|
||||
|
||||
for (i = 0; i < n; i++)
|
||||
if (cp[i] == pixel) {
|
||||
*r = cr[i][0]; *g = cr[i][1]; *b = cr[i][2];
|
||||
return 0;
|
||||
}
|
||||
c.pixel = pixel;
|
||||
if (!XQueryColor(dpy, DefaultColormap(dpy, 0), &c))
|
||||
return -1;
|
||||
if (truecolor)
|
||||
return ((unsigned long) (r * rmax / 255) << rshift) |
|
||||
((unsigned long) (g * gmax / 255) << gshift) |
|
||||
((unsigned long) (b * bmax / 255) << bshift);
|
||||
c.red = (unsigned short) (r * 257);
|
||||
c.green = (unsigned short) (g * 257);
|
||||
c.blue = (unsigned short) (b * 257);
|
||||
c.flags = DoRed | DoGreen | DoBlue;
|
||||
c.pixel = 0;
|
||||
if (!XAllocColor(dpy, cmap, &c))
|
||||
return 0;
|
||||
return c.pixel;
|
||||
}
|
||||
|
||||
static void
|
||||
PixelToRGB(unsigned long p, unsigned char *r, unsigned char *g, unsigned char *b)
|
||||
{
|
||||
XColor c;
|
||||
|
||||
if (truecolor) {
|
||||
*r = (unsigned char) (((p & rmask) >> rshift) *
|
||||
255 / (rmax ? rmax : 1));
|
||||
*g = (unsigned char) (((p & gmask) >> gshift) *
|
||||
255 / (gmax ? gmax : 1));
|
||||
*b = (unsigned char) (((p & bmask) >> bshift) *
|
||||
255 / (bmax ? bmax : 1));
|
||||
return;
|
||||
}
|
||||
c.pixel = p;
|
||||
if (!XQueryColor(dpy, cmap, &c)) {
|
||||
*r = *g = *b = 0;
|
||||
return;
|
||||
}
|
||||
*r = (unsigned char) (c.red >> 8);
|
||||
*g = (unsigned char) (c.green >> 8);
|
||||
*b = (unsigned char) (c.blue >> 8);
|
||||
if (n < 64) {
|
||||
cp[n] = pixel;
|
||||
cr[n][0] = *r; cr[n][1] = *g; cr[n][2] = *b;
|
||||
n++;
|
||||
}
|
||||
|
||||
static int
|
||||
CmpUL(const void *a, const void *b)
|
||||
{
|
||||
unsigned long x = *(const unsigned long *) a;
|
||||
unsigned long y = *(const unsigned long *) b;
|
||||
|
||||
return (x < y) ? -1 : (x > y) ? 1 : 0;
|
||||
}
|
||||
|
||||
/* sort KEYS in place and drop the repeats; the number left */
|
||||
static int
|
||||
Unique(unsigned long *keys, int n)
|
||||
{
|
||||
int i, j = 0;
|
||||
|
||||
qsort(keys, (size_t) n, sizeof(unsigned long), CmpUL);
|
||||
for (i = 0; i < n; i++)
|
||||
if (i == 0 || keys[i] != keys[i - 1])
|
||||
keys[j++] = keys[i];
|
||||
return j;
|
||||
}
|
||||
|
||||
/* the RGB of N distinct pixels, in one XQueryColors instead of N round trips
|
||||
* (PseudoColor only; on TrueColor PixelToRGB needs no server at all) */
|
||||
static void
|
||||
BatchQuery(unsigned long *pix, int n, unsigned char *rgb)
|
||||
{
|
||||
XColor *cols;
|
||||
int i;
|
||||
|
||||
if ((cols = malloc(sizeof(XColor) * (size_t) n)) == NULL)
|
||||
return;
|
||||
for (i = 0; i < n; i++)
|
||||
cols[i].pixel = pix[i];
|
||||
if (XQueryColors(dpy, cmap, cols, n))
|
||||
for (i = 0; i < n; i++) {
|
||||
rgb[i * 3] = (unsigned char) (cols[i].red >> 8);
|
||||
rgb[i * 3 + 1] = (unsigned char) (cols[i].green >> 8);
|
||||
rgb[i * 3 + 2] = (unsigned char) (cols[i].blue >> 8);
|
||||
}
|
||||
free(cols);
|
||||
}
|
||||
|
||||
/* the RGB as 0xRRGGBB keys of the distinct colours, ascending; the count */
|
||||
static int
|
||||
DistinctColors(const unsigned char *rgb, int n, unsigned long **keyp)
|
||||
{
|
||||
unsigned long *keys;
|
||||
int i;
|
||||
|
||||
if ((keys = malloc(sizeof(unsigned long) * (size_t) n)) == NULL) {
|
||||
*keyp = NULL;
|
||||
return 0;
|
||||
}
|
||||
for (i = 0; i < n; i++)
|
||||
keys[i] = ((unsigned long) rgb[i * 3] << 16) |
|
||||
((unsigned long) rgb[i * 3 + 1] << 8) | rgb[i * 3 + 2];
|
||||
*keyp = keys;
|
||||
return Unique(keys, n);
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ files */
|
||||
|
||||
/* the document from an 8-bit RGB buffer (W x H, rows tight, top-down): the
|
||||
* image and the undo snapshot, and the status line. On TrueColor the pixels
|
||||
* are arithmetic on the masks; on PseudoColor one XAllocColor per *distinct*
|
||||
* colour, never per pixel. */
|
||||
static int
|
||||
SetImageRGB(const unsigned char *rgb, int w, int h)
|
||||
{
|
||||
XImage *im;
|
||||
unsigned long *keys = NULL, *kpix = NULL;
|
||||
int nkeys = 0, x, y;
|
||||
|
||||
if (w < 1 || h < 1 || w > 8192 || h > 8192)
|
||||
return -1;
|
||||
docW = w;
|
||||
docH = h;
|
||||
NewPixmaps();
|
||||
im = XCreateImage(dpy, vis, (unsigned int) visdepth, ZPixmap, 0, NULL,
|
||||
(unsigned int) w, (unsigned int) h, 32, 0);
|
||||
if (im == NULL)
|
||||
return -1;
|
||||
if ((im->data = calloc(1, (size_t) im->bytes_per_line * h)) == NULL) {
|
||||
XDestroyImage(im);
|
||||
return -1;
|
||||
}
|
||||
if (!truecolor) {
|
||||
nkeys = DistinctColors(rgb, w * h, &keys);
|
||||
if (nkeys > 0 && (kpix = malloc(sizeof(unsigned long) *
|
||||
(size_t) nkeys)) != NULL) {
|
||||
unsigned char *krgb = malloc((size_t) nkeys * 3);
|
||||
|
||||
if (krgb != NULL) {
|
||||
int i;
|
||||
|
||||
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];
|
||||
}
|
||||
for (i = 0; i < nkeys; i++)
|
||||
kpix[i] = RGBToPixel(krgb[i * 3],
|
||||
krgb[i * 3 + 1], krgb[i * 3 + 2]);
|
||||
free(krgb);
|
||||
} else {
|
||||
free(kpix);
|
||||
kpix = NULL;
|
||||
}
|
||||
}
|
||||
}
|
||||
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 px;
|
||||
|
||||
if (truecolor) {
|
||||
px = RGBToPixel(p[0], p[1], p[2]);
|
||||
} else if (keys != NULL && kpix != NULL) {
|
||||
unsigned long key =
|
||||
((unsigned long) p[0] << 16) |
|
||||
((unsigned long) p[1] << 8) | p[2];
|
||||
unsigned long *hit = bsearch(&key, keys,
|
||||
(size_t) nkeys, sizeof(unsigned long),
|
||||
CmpUL);
|
||||
|
||||
px = (hit != NULL) ? kpix[hit - keys]
|
||||
: RGBToPixel(p[0], p[1], p[2]);
|
||||
} else {
|
||||
px = RGBToPixel(p[0], p[1], p[2]);
|
||||
}
|
||||
XPutPixel(im, x, y, px);
|
||||
}
|
||||
free(keys);
|
||||
free(kpix);
|
||||
XPutImage(dpy, image, gc, im, 0, 0, 0, 0, (unsigned int) w,
|
||||
(unsigned int) h);
|
||||
XDestroyImage(im);
|
||||
XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -127,35 +337,83 @@ PutLE(unsigned char *p, unsigned long v, int n)
|
||||
p[i] = (unsigned char) ((v >> (8 * i)) & 0xff);
|
||||
}
|
||||
|
||||
/* the document as 8-bit RGB, rows top-down and tight: one XGetImage and,
|
||||
* off TrueColor, one XQueryColors for the distinct pixels -- never one
|
||||
* XQueryColor per pixel. The caller frees. */
|
||||
static int
|
||||
GetImageRGB(unsigned char **rgbp, int *wp, int *hp)
|
||||
{
|
||||
XImage *im;
|
||||
unsigned char *rgb;
|
||||
unsigned long *keys = NULL;
|
||||
unsigned char *krgb = NULL;
|
||||
int nkeys = 0, x, y;
|
||||
|
||||
if (docW < 1 || docH < 1)
|
||||
return -1;
|
||||
if ((im = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes,
|
||||
ZPixmap)) == NULL)
|
||||
return -1;
|
||||
if ((rgb = malloc((size_t) docW * docH * 3)) == NULL) {
|
||||
XDestroyImage(im);
|
||||
return -1;
|
||||
}
|
||||
if (!truecolor) {
|
||||
if ((keys = malloc(sizeof(unsigned long) *
|
||||
(size_t) docW * docH)) != NULL) {
|
||||
for (y = 0; y < docH; y++)
|
||||
for (x = 0; x < docW; x++)
|
||||
keys[nkeys++] = XGetPixel(im, x, y);
|
||||
nkeys = Unique(keys, nkeys);
|
||||
if ((krgb = malloc((size_t) nkeys * 3)) != NULL)
|
||||
BatchQuery(keys, nkeys, krgb);
|
||||
}
|
||||
}
|
||||
for (y = 0; y < docH; y++)
|
||||
for (x = 0; x < docW; x++) {
|
||||
unsigned char *d = rgb + ((size_t) y * docW + x) * 3;
|
||||
unsigned long px = XGetPixel(im, x, y);
|
||||
|
||||
if (truecolor) {
|
||||
PixelToRGB(px, &d[0], &d[1], &d[2]);
|
||||
} else if (keys != NULL && krgb != NULL) {
|
||||
unsigned long *hit = bsearch(&px, keys,
|
||||
(size_t) nkeys, sizeof(unsigned long),
|
||||
CmpUL);
|
||||
|
||||
if (hit == NULL) {
|
||||
d[0] = d[1] = d[2] = 0;
|
||||
continue;
|
||||
}
|
||||
d[0] = krgb[(hit - keys) * 3];
|
||||
d[1] = krgb[(hit - keys) * 3 + 1];
|
||||
d[2] = krgb[(hit - keys) * 3 + 2];
|
||||
} else {
|
||||
PixelToRGB(px, &d[0], &d[1], &d[2]);
|
||||
}
|
||||
}
|
||||
free(keys);
|
||||
free(krgb);
|
||||
XDestroyImage(im);
|
||||
*rgbp = rgb;
|
||||
*wp = docW;
|
||||
*hp = docH;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int
|
||||
SaveBMP(const char *path)
|
||||
{
|
||||
FILE *f;
|
||||
XImage *im;
|
||||
unsigned char *rgb, hdr[54];
|
||||
int row = ((docW * 3 + 3) / 4) * 4;
|
||||
unsigned long size = (unsigned long) row * docH;
|
||||
unsigned char *data;
|
||||
unsigned char hdr[54];
|
||||
int x, y;
|
||||
|
||||
if ((im = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes, ZPixmap)) == NULL)
|
||||
if (GetImageRGB(&rgb, &x, &y) != 0)
|
||||
return -1;
|
||||
if ((data = calloc(1, (size_t) size)) == NULL) {
|
||||
XDestroyImage(im);
|
||||
return -1;
|
||||
}
|
||||
for (y = 0; y < docH; y++)
|
||||
for (x = 0; x < docW; x++) {
|
||||
unsigned char r, g, b;
|
||||
unsigned char *p = data + (size_t) (docH - 1 - y) * row + x * 3;
|
||||
|
||||
if (PixelRGB(XGetPixel(im, x, y), &r, &g, &b) < 0)
|
||||
continue;
|
||||
p[0] = b; p[1] = g; p[2] = r;
|
||||
}
|
||||
XDestroyImage(im);
|
||||
if ((f = fopen(path, "wb")) == NULL) {
|
||||
free(data);
|
||||
free(rgb);
|
||||
return -1;
|
||||
}
|
||||
memset(hdr, 0, sizeof hdr);
|
||||
@@ -170,86 +428,439 @@ SaveBMP(const char *path)
|
||||
PutLE(hdr + 34, size, 4);
|
||||
PutLE(hdr + 38, 2835, 4);
|
||||
PutLE(hdr + 42, 2835, 4);
|
||||
fwrite(hdr, 1, sizeof hdr, f);
|
||||
fwrite(data, 1, (size_t) size, f);
|
||||
fclose(f);
|
||||
free(data);
|
||||
if (fwrite(hdr, 1, sizeof hdr, f) != sizeof hdr) {
|
||||
fclose(f);
|
||||
free(rgb);
|
||||
return -1;
|
||||
}
|
||||
for (y = docH - 1; y >= 0; y--) {
|
||||
const unsigned char *s = rgb + (size_t) y * docW * 3;
|
||||
unsigned char *line = malloc((size_t) row);
|
||||
|
||||
if (line == NULL) {
|
||||
fclose(f);
|
||||
free(rgb);
|
||||
return -1;
|
||||
}
|
||||
memset(line, 0, (size_t) row);
|
||||
for (x = 0; x < docW; x++) {
|
||||
line[x * 3] = s[x * 3 + 2];
|
||||
line[x * 3 + 1] = s[x * 3 + 1];
|
||||
line[x * 3 + 2] = s[x * 3];
|
||||
}
|
||||
if (fwrite(line, 1, (size_t) row, f) != (size_t) row) {
|
||||
free(line);
|
||||
fclose(f);
|
||||
free(rgb);
|
||||
return -1;
|
||||
}
|
||||
free(line);
|
||||
}
|
||||
free(rgb);
|
||||
if (fclose(f) != 0)
|
||||
return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void NewPixmaps(void);
|
||||
static void DrawMarquee(Window win, int ox, int oy);
|
||||
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);
|
||||
}
|
||||
|
||||
/* a 24-bit BMP (BITMAPINFOHEADER, uncompressed, bottom-up or top-down) */
|
||||
static int
|
||||
OpenBMP24(const char *path)
|
||||
{
|
||||
FILE *f;
|
||||
unsigned char hdr[54];
|
||||
unsigned long off, row, size;
|
||||
long w, h, hs;
|
||||
unsigned char *data, *rgb;
|
||||
int x, y, topdown = 0;
|
||||
|
||||
if ((f = fopen(path, "rb")) == NULL)
|
||||
return -1;
|
||||
if (fread(hdr, 1, 54, f) != 54 || hdr[0] != 'B' || hdr[1] != 'M' ||
|
||||
LE32(hdr + 14) != 40 || LE16(hdr + 28) != 24 ||
|
||||
LE32(hdr + 30) != 0) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
off = LE32(hdr + 10);
|
||||
w = (long) LE32(hdr + 18);
|
||||
hs = (long) (int) LE32(hdr + 22);
|
||||
if (hs < 0) {
|
||||
topdown = 1;
|
||||
h = -hs;
|
||||
} else {
|
||||
h = hs;
|
||||
}
|
||||
if (w < 1 || h < 1 || w > 8192 || h > 8192) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
row = (((unsigned long) w * 24 + 31) / 32) * 4;
|
||||
size = row * (unsigned long) h;
|
||||
if ((data = malloc((size_t) size)) == NULL) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (fseek(f, (long) off, SEEK_SET) != 0 ||
|
||||
fread(data, 1, (size_t) size, f) != (size_t) size) {
|
||||
free(data);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
if ((rgb = malloc((size_t) w * h * 3)) == NULL) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
for (y = 0; y < h; y++) {
|
||||
const unsigned char *s = data + (topdown ? (long) y :
|
||||
h - 1 - y) * row;
|
||||
|
||||
for (x = 0; x < w; x++) {
|
||||
unsigned char *d = rgb + ((size_t) y * w + x) * 3;
|
||||
|
||||
d[0] = s[x * 3 + 2];
|
||||
d[1] = s[x * 3 + 1];
|
||||
d[2] = s[x * 3];
|
||||
}
|
||||
}
|
||||
free(data);
|
||||
x = SetImageRGB(rgb, (int) w, (int) h);
|
||||
free(rgb);
|
||||
return x;
|
||||
}
|
||||
|
||||
/* 8-bit palette BMP, uncompressed or RLE8 */
|
||||
static int
|
||||
OpenBMP8(const char *path)
|
||||
{
|
||||
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 ((f = fopen(path, "rb")) == NULL)
|
||||
return -1;
|
||||
if (fread(fh, 1, 14, f) != 14 || fh[0] != 'B' || fh[1] != 'M' ||
|
||||
fread(ih, 1, 40, f) != 40 || LE32(ih) != 40 ||
|
||||
LE16(ih + 12) != 1) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
bpp = (int) LE16(ih + 14);
|
||||
comp = (int) LE32(ih + 16);
|
||||
if (bpp != 8 || (comp != 0 && comp != 1)) {
|
||||
fclose(f);
|
||||
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 > 8192 || h > 8192) {
|
||||
fclose(f);
|
||||
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);
|
||||
for (i = 0; i < ncolors; i++) {
|
||||
unsigned char e[4];
|
||||
|
||||
if (fread(e, 1, 4, f) != 4) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
pal[i][0] = e[2];
|
||||
pal[i][1] = e[1];
|
||||
pal[i][2] = e[0];
|
||||
}
|
||||
if ((idx = calloc((size_t) w * h, 1)) == NULL) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (comp == 0) {
|
||||
row = (((unsigned long) w * 8 + 31) / 32) * 4;
|
||||
if ((raw = malloc((size_t) row * h)) == NULL) {
|
||||
free(idx);
|
||||
fclose(f);
|
||||
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);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
for (y = 0; y < h; y++)
|
||||
memcpy(idx + (size_t) y * w,
|
||||
raw + (topdown ? y : (h - 1 - y)) * row, (size_t) w);
|
||||
free(raw);
|
||||
} else {
|
||||
/* RLE8: count/value, 00 00 end of line, 00 01 end of bitmap,
|
||||
* 00 02 dx dy delta, 00 n literals padded to a word */
|
||||
long end;
|
||||
unsigned char *rl = NULL;
|
||||
size_t p = 0;
|
||||
|
||||
if (fseek(f, 0, SEEK_END) != 0 || (end = ftell(f)) < 0 ||
|
||||
fseek(f, (long) off, SEEK_SET) != 0) {
|
||||
free(idx);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
size = (unsigned long) (end - (long) off);
|
||||
if (size > 64UL * 1024 * 1024 ||
|
||||
(rl = malloc((size_t) size)) == NULL ||
|
||||
fread(rl, 1, (size_t) size, f) != (size_t) size) {
|
||||
free(rl);
|
||||
free(idx);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
x = 0;
|
||||
y = topdown ? 0 : (int) h - 1;
|
||||
while (p + 1 < size) {
|
||||
int cnt = rl[p], val = rl[p + 1], k;
|
||||
|
||||
p += 2;
|
||||
if (cnt > 0) {
|
||||
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 {
|
||||
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);
|
||||
}
|
||||
if ((rgb = malloc((size_t) w * h * 3)) == NULL) {
|
||||
free(idx);
|
||||
fclose(f);
|
||||
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);
|
||||
fclose(f);
|
||||
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; an icon whose
|
||||
* pixels are a PNG is refused (there is no PNG reader in this program) */
|
||||
static int
|
||||
OpenICO(const char *path)
|
||||
{
|
||||
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 char br, bg, bb;
|
||||
|
||||
if ((f = fopen(path, "rb")) == NULL)
|
||||
return -1;
|
||||
if (fread(dir, 1, 6, f) != 6 || LE16(dir) != 0 || LE16(dir + 2) != 1 ||
|
||||
LE16(dir + 4) < 1 || fread(ent, 1, 16, f) != 16) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
off = LE32(ent + 12);
|
||||
if (off < 22 || fseek(f, (long) off, SEEK_SET) != 0 ||
|
||||
fread(ih, 1, 40, f) != 40) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (ih[0] == 0x89 && ih[1] == 'P' && ih[2] == 'N' && ih[3] == 'G') {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
if (LE32(ih) != 40 || LE16(ih + 12) != 1 || LE32(ih + 16) != 0) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
w = (long) LE32(ih + 4);
|
||||
hs = (long) (int) LE32(ih + 8);
|
||||
if (hs <= 0 || (hs % 2) != 0 || w < 1 || w > 8192 || hs / 2 > 8192) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
h = hs / 2;
|
||||
bpp = (int) LE16(ih + 14);
|
||||
if (bpp != 1 && bpp != 4 && bpp != 8 && bpp != 24 && bpp != 32) {
|
||||
fclose(f);
|
||||
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) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
pal[i][0] = e[2];
|
||||
pal[i][1] = e[1];
|
||||
pal[i][2] = e[0];
|
||||
}
|
||||
row = (((unsigned long) w * (unsigned long) bpp + 31) / 32) * 4;
|
||||
if ((raw = malloc((size_t) row * h)) == NULL ||
|
||||
fread(raw, 1, (size_t) row * h, f) != (size_t) row * h) {
|
||||
free(raw);
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
if ((rgb = malloc((size_t) w * h * 3)) == NULL) {
|
||||
free(raw);
|
||||
return -1;
|
||||
}
|
||||
/* an icon's transparent pixel is the background of the document, as
|
||||
* dtpaint composes it (ext/src/mcde/AGENTS.md) */
|
||||
PixelToRGB(bgPixel, &br, &bg, &bb);
|
||||
for (y = 0; y < h; y++) {
|
||||
const unsigned char *s = raw + (size_t) (h - 1 - y) * row;
|
||||
|
||||
for (x = 0; x < w; x++) {
|
||||
unsigned char *d = rgb + ((size_t) y * w + x) * 3;
|
||||
|
||||
if (bpp == 32) {
|
||||
d[0] = s[x * 4 + 2];
|
||||
d[1] = s[x * 4 + 1];
|
||||
d[2] = s[x * 4];
|
||||
if (s[x * 4 + 3] == 0) {
|
||||
d[0] = br;
|
||||
d[1] = bg;
|
||||
d[2] = bb;
|
||||
}
|
||||
} else if (bpp == 24) {
|
||||
d[0] = s[x * 3 + 2];
|
||||
d[1] = s[x * 3 + 1];
|
||||
d[2] = s[x * 3];
|
||||
} else {
|
||||
int idx;
|
||||
|
||||
if (bpp == 8)
|
||||
idx = s[x];
|
||||
else if (bpp == 4)
|
||||
idx = (x & 1) ? (s[x / 2] & 15) :
|
||||
(s[x / 2] >> 4);
|
||||
else
|
||||
idx = (s[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;
|
||||
}
|
||||
|
||||
/* a BMP (24 or 8 bit) or the first image of an ICO */
|
||||
static int
|
||||
OpenBMP(const char *path)
|
||||
{
|
||||
FILE *f;
|
||||
unsigned char hdr[54];
|
||||
unsigned long off, w, h, row;
|
||||
unsigned char *data;
|
||||
XImage *im;
|
||||
int x, y;
|
||||
unsigned char m[6], hdr[54];
|
||||
int bpp;
|
||||
|
||||
if ((f = fopen(path, "rb")) == NULL)
|
||||
return -1;
|
||||
if (fread(hdr, 1, 54, f) != 54 || hdr[0] != 'B' || hdr[1] != 'M') {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
off = hdr[10] | (hdr[11] << 8) | (hdr[12] << 16) | (hdr[13] << 24);
|
||||
w = hdr[18] | (hdr[19] << 8) | (hdr[20] << 16) | (hdr[21] << 24);
|
||||
h = hdr[22] | (hdr[23] << 8) | (hdr[24] << 16) | (hdr[25] << 24);
|
||||
if (hdr[28] != 24 || w == 0 || h == 0 || w > 4096 || h > 4096) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
row = ((w * 3 + 3) / 4) * 4;
|
||||
if ((data = malloc(row * h)) == NULL) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fseek(f, (long) off, SEEK_SET);
|
||||
if (fread(data, 1, row * h, f) != row * h) {
|
||||
free(data);
|
||||
if (fread(m, 1, 6, f) != 6) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
fclose(f);
|
||||
|
||||
docW = (int) w;
|
||||
docH = (int) h;
|
||||
NewPixmaps();
|
||||
im = XCreateImage(dpy, DefaultVisual(dpy, 0), DefaultDepth(dpy, 0),
|
||||
ZPixmap, 0, NULL, (unsigned int) w, (unsigned int) h, 32, 0);
|
||||
if (im == NULL) {
|
||||
free(data);
|
||||
return -1;
|
||||
}
|
||||
im->data = calloc(1, (size_t) im->bytes_per_line * h);
|
||||
if (im->data == NULL) {
|
||||
free(data);
|
||||
XDestroyImage(im);
|
||||
return -1;
|
||||
}
|
||||
for (y = 0; y < (int) h; y++)
|
||||
for (x = 0; x < (int) w; x++) {
|
||||
unsigned char *p = data + (size_t) (h - 1 - y) * row + x * 3;
|
||||
XColor c;
|
||||
|
||||
c.red = (unsigned short) (p[2] << 8);
|
||||
c.green = (unsigned short) (p[1] << 8);
|
||||
c.blue = (unsigned short) (p[0] << 8);
|
||||
c.flags = DoRed | DoGreen | DoBlue;
|
||||
if (!XAllocColor(dpy, DefaultColormap(dpy, 0), &c))
|
||||
continue;
|
||||
XPutPixel(im, x, y, c.pixel);
|
||||
if (m[0] == 'B' && m[1] == 'M') {
|
||||
if ((f = fopen(path, "rb")) == NULL)
|
||||
return -1;
|
||||
if (fread(hdr, 1, 54, f) != 54 || LE32(hdr + 14) != 40) {
|
||||
fclose(f);
|
||||
return -1;
|
||||
}
|
||||
XPutImage(dpy, image, gc, im, 0, 0, 0, 0, (unsigned int) w,
|
||||
(unsigned int) h);
|
||||
XDestroyImage(im);
|
||||
free(data);
|
||||
XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0);
|
||||
return 0;
|
||||
fclose(f);
|
||||
bpp = (int) LE16(hdr + 28);
|
||||
if (bpp == 24)
|
||||
return OpenBMP24(path);
|
||||
if (bpp == 8)
|
||||
return OpenBMP8(path);
|
||||
return -1;
|
||||
}
|
||||
if (m[0] == 0 && m[1] == 0 && m[2] == 1 && m[3] == 0)
|
||||
return OpenICO(path);
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* ------------------------------------------------------------------ menus */
|
||||
@@ -459,12 +1070,12 @@ NewPixmaps(void)
|
||||
XFreePixmap(dpy, image);
|
||||
if (snap != None)
|
||||
XFreePixmap(dpy, snap);
|
||||
image = XCreatePixmap(dpy, DefaultRootWindow(dpy), docW, docH,
|
||||
DefaultDepth(dpy, 0));
|
||||
snap = XCreatePixmap(dpy, DefaultRootWindow(dpy), docW, docH,
|
||||
DefaultDepth(dpy, 0));
|
||||
image = XCreatePixmap(dpy, drawable, docW, docH,
|
||||
(unsigned int) visdepth);
|
||||
snap = XCreatePixmap(dpy, drawable, docW, docH,
|
||||
(unsigned int) visdepth);
|
||||
if (gc != None) {
|
||||
XSetForeground(dpy, gc, WhitePixel(dpy, 0));
|
||||
XSetForeground(dpy, gc, bgPixel);
|
||||
XFillRectangle(dpy, image, gc, 0, 0, docW, docH);
|
||||
}
|
||||
}
|
||||
@@ -639,8 +1250,7 @@ CropToSelection(void)
|
||||
h = selY1 - selY0;
|
||||
if (w <= 0 || h <= 0)
|
||||
return;
|
||||
fresh = XCreatePixmap(dpy, DefaultRootWindow(dpy), w, h,
|
||||
DefaultDepth(dpy, 0));
|
||||
fresh = XCreatePixmap(dpy, drawable, w, h, (unsigned int) visdepth);
|
||||
XCopyArea(dpy, image, fresh, gc, selX0, selY0, (unsigned int) w,
|
||||
(unsigned int) h, 0, 0);
|
||||
XFreePixmap(dpy, image);
|
||||
@@ -648,8 +1258,8 @@ CropToSelection(void)
|
||||
docW = w;
|
||||
docH = h;
|
||||
image = fresh;
|
||||
snap = XCreatePixmap(dpy, DefaultRootWindow(dpy), docW, docH,
|
||||
DefaultDepth(dpy, 0));
|
||||
snap = XCreatePixmap(dpy, drawable, docW, docH,
|
||||
(unsigned int) visdepth);
|
||||
hasSel = 0;
|
||||
UpdateStatus();
|
||||
RenderDoc(canvas);
|
||||
@@ -1140,6 +1750,7 @@ main(int argc, char **argv)
|
||||
top = XtVaAppInitialize(&app, "Dtphoto", opts, XtNumber(opts), &argc,
|
||||
argv, fallbacks, NULL);
|
||||
dpy = XtDisplay(top);
|
||||
InitColor();
|
||||
if (XGetDefault(dpy, "dtphoto", "version") != NULL)
|
||||
snprintf(version, sizeof version, "%s",
|
||||
XGetDefault(dpy, "dtphoto", "version"));
|
||||
@@ -1224,13 +1835,18 @@ main(int argc, char **argv)
|
||||
printf("dtphoto selftest: FAIL image\n");
|
||||
return 1;
|
||||
}
|
||||
for (y = 0; y < docH && ok; y += 37)
|
||||
for (x = 0; x < docW; x += 41)
|
||||
if (XGetPixel(a, x, y) != XGetPixel(b, x, y))
|
||||
/* every pixel, not a sample: a colour that survives one point
|
||||
* can be lost at another (2026-11-22) */
|
||||
for (y = 0; y < docH && ok; y++)
|
||||
for (x = 0; x < docW; x++)
|
||||
if (XGetPixel(a, x, y) != XGetPixel(b, x, y)) {
|
||||
ok = 0;
|
||||
break;
|
||||
}
|
||||
XDestroyImage(a);
|
||||
XDestroyImage(b);
|
||||
printf("dtphoto selftest: %s\n", ok ? "PASS" : "FAIL");
|
||||
printf("dtphoto selftest: %s (%dx%d)\n", ok ? "PASS" : "FAIL",
|
||||
docW, docH);
|
||||
return ok ? 0 : 1;
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user