MCDE: PNG, JPEG and GIF in dtphoto, one picture writer in libDtSvc

dtphoto read only BMP and ICO and wrote only BMP; PNG, JPEG and GIF were
listed as open in docs/MCDE-PROGRAMS.md since 2026-11-12.  The reader was
already there: McdeThumbRead in libDtSvc has all five formats, is X-free and
is what dtfile uses for its miniatures.  dtphoto calls it for everything its
own reader does not know, so the program carries no second decoder.

There was no picture writer anywhere in the tree, so McdeThumbWrite now sits
next to the reader: PNG through libpng's simplified API, BMP in C99, the
ending decides, an unknown ending is refused instead of silently becoming a
BMP.

Measured on Xvfb depth 24 (2026-11-22):
- 40x30 PNG -> PNG, 1200 of 1200 pixels identical, CRC checked.
- 40x30 GIF -> PNG, 0 of 1200 pixels different.
- 40x30 JPEG q100 -> PNG, at most 3 steps per channel (compression, not the
  reader); the same picture written from libpng is 0 steps.
- Depth 8: a 32-colour picture comes back 0 of 256 pixels different; a
  1200-colour ramp differs by mean 28.6/255, which is the colormap -- the
  same ramp is 0 on depth 24.

test_thumb writes a 13x7 ramp (every pixel unique) as PNG and BMP and reads
it back pixel for pixel.  Proved the test bites: writing the BMP rows
top-down turns it red at 78 of 91 pixels, PNG_FORMAT_BGR at 91 of 91.  The
unknown-ending and zero-size cases are checked too.

dtphoto: the file dialog filters on every format it reads, not on *.bmp.
All 18 dtphoto test modes stay green on depth 24 and depth 8, test_thumb ok.
This commit is contained in:
lotherk committed 2026-11-22 12:06:04 +00:00
1 parent e0f4867ded
commit 0d0a144d66
7 files changed
+1774 -39

No files matched your search

+45 -2
View File
@@ -132,6 +132,36 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt
The zoom magnifier doubles on the left button and halves on the right
(12%-800%), the hand pans the view, and both say what they did on
stderr so a mouse-driven test can read the result.
The Select menu's morphology works on an exact euclidean distance field
(two passes, then the distance to the nearest selected and the nearest
unselected pixel), so Expand, Contract, Border, Smooth, Grow, Similar
and Feather are arithmetic, not guesses. Feather needed a second
layer: one bit per pixel cannot carry a soft edge, so `selCover` holds
0-255 beside the bit mask and every consumer -- `OpPerPixel`, the
gradient, `PaintMask` -- mixes by that coverage instead of cutting at
the bit. `-selmodtest` checks the numbers (a 5x5 block contracted by 1
loses its four corners and nothing else, a 2 px band from Border, Grow
taking the neighbouring colour and Similar the whole picture).
`Window > Show Options` opens the Options box, which is where the
tool parameters live: pencil and eraser radius, the wand's tolerance
(which Grow/Similar read too), the retouch radius and strength, and the
gradient as linear or radial. Every field writes straight into the
variable the event handler reads, so there is no second copy, and each
edit says `dtphoto: options <name>=<value>` on stderr. The box is built
by a fixed row list, and a row returns the FIELD, not its label: a text
field is taller than its label, so attaching the next row to the label
stacked the fields on top of each other and the tolerance field was
unreachable (measured 2026-11-22, the click landed in the eraser field).
`DTPHOTO_OPTIONS=1` opens the box at startup, which is how the test
reaches it without a mouse.
Painting is clipped to the selection, as in the original: the pencil
(a hard square tip of 2r+1), the brush (a round one) and the eraser all
go through `PaintTip`, which reads the box around the dab once, mixes by
the coverage and writes it back. Before that the pencil drew its tip
over the whole picture whatever the mask said, and the pencil ignored
its radius entirely (one XDrawPoint). `-optest` proves both: a tip laid
across the edge of a 2 px selection changes only those two columns, and
the wand at tolerance 0 takes one pixel where tolerance 100 takes six.
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);
@@ -158,7 +188,17 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt
undo across it); Edit Colors sets the foreground from three RGB scales;
BMP 8-bit (uncompressed and RLE8) and the first image of an ICO
(1/4/8/24/32 bpp, alpha 0 composited on the background; PNG-compressed
icons refused) are read, saving stays 24-bit BMP; Print spools the
icons refused) go through the program's own reader; PNG, JPEG, GIF and
XPM come from `McdeThumbRead` (libDtSvc), so the program carries no
second decoder. Saving writes the format the name ends in, `.png` and
`.bmp` through `McdeThumbWrite`, anything else as 24-bit BMP. Measured
2026-11-22 on Xvfb depth 24: a 40x30 PNG written back is 1200 of 1200
pixels identical, a GIF 0 of 1200 different, JPEG q100 at most 3 steps
per channel; on depth 8 a 32-colour picture comes back exact and the
1200-colour ramp is off by the colormap (mean 28.6 of 255), which is the
visual, not the reader. `test_thumb` writes a 13x7 ramp and reads it
back pixel for pixel, and turns red when the writer's rows are flipped or
its channels swapped. Print spools the
current image through `mcde_sys_print_file` (lp; MCDE_PRINT_COMMAND fakes
the tool in tests) and removes the temporary file when the job ends.
McdeSys has a catalog too (`lib/McdeSys/mcdesys.msg`, `lib/McdeSys/
@@ -192,7 +232,10 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt
test `test_thumb`) is the miniature: PNG, JPEG, GIF, BMP and XPM read with
libpng, libjpeg and C99, scaled with a box filter and cached in
`~/.dt/thumbs/<64|128|256>/` -- the one picture reader for dtfile and the
programs that show pictures, no gdk-pixbuf, no program per picture.
programs that show pictures, no gdk-pixbuf, no program per picture. Since
2026-11-22 it writes too: `McdeThumbWrite` (PNG through libpng's simplified
API, BMP in C99, the ending decides), so an editor saves without a second
encoder.
- Programs of its own: `dtbar` (top bar and global menu, protocol
`include/Mcde/GlobalMenu.h`), `dtkeys`, `dtthemesync`, `dtappscan`,
`dtsystemsettings` (panes by category, McdeSys only), `dtdiskutility`,
+8
View File
@@ -51,6 +51,14 @@ void McdeThumbFree(McdeThumbImage *image);
int McdeThumbScale(const unsigned char *src, int sw, int sh, int maxw,
int maxh, unsigned char **dst, int *w, int *h);
/* IMAGE (8-bit RGB) written to PATH. The format comes from the name's
* ending: .png and .bmp are written, anything else is refused. A picture
* written here is read back by McdeThumbRead() pixel for pixel, which is
* what the test checks. 0 on success, -1 on a bad argument, an unknown
* ending or a write error. */
int McdeThumbWrite(const char *path, const unsigned char *rgb, int w,
int h);
/* IMAGE on SCREEN as a pixmap of the screen's depth; the size is the
* picture's, in pixels. 0 on success, -1 when the picture cannot be read or
* the visual is not one this module writes (PseudoColor is not). */
+121
View File
@@ -29,6 +29,7 @@
#include <sys/stat.h>
#include <sys/time.h>
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <limits.h>
@@ -908,6 +909,15 @@ bad:
/* ------------------------------------------------------------------ */
/* BMP: BI_RGB, 8 and 24 and 32 bits */
static void
Le32Put(unsigned char *p, unsigned long v)
{
p[0] = (unsigned char) (v & 0xff);
p[1] = (unsigned char) ((v >> 8) & 0xff);
p[2] = (unsigned char) ((v >> 16) & 0xff);
p[3] = (unsigned char) ((v >> 24) & 0xff);
}
static unsigned long
Le32(const unsigned char *p)
{
@@ -1551,3 +1561,114 @@ McdeThumbScale(const unsigned char *src, int sw, int sh, int maxw, int maxh,
return -1;
return ScaleTo(src, sw, sh, maxw, maxh, dst, w, h);
}
/* ------------------------------------------------------------------ */
/* writing a picture, the reverse of the readers above */
/* the ending of PATH, lower case, without the dot; NULL when there is none */
static const char *
Ending(const char *path)
{
const char *dot = strrchr(path, '.');
if (dot == NULL || dot[1] == '\0')
return NULL;
return dot + 1;
}
static int
SameEnding(const char *path, const char *want)
{
const char *e = Ending(path);
size_t i;
if (e == NULL)
return 0;
for (i = 0; want[i] != '\0'; i++)
if (tolower((unsigned char) e[i]) != want[i])
return 0;
return e[i] == '\0';
}
static int
WritePng(const char *path, const unsigned char *rgb, int w, int h)
{
png_image img;
memset(&img, 0, sizeof img);
img.version = PNG_IMAGE_VERSION;
img.width = (png_uint_32) w;
img.height = (png_uint_32) h;
img.format = PNG_FORMAT_RGB;
if (!png_image_write_to_file(&img, path, 0, rgb, 0, NULL)) {
png_image_free(&img);
return -1;
}
png_image_free(&img);
return 0;
}
static int
WriteBmp(const char *path, const unsigned char *rgb, int w, int h)
{
FILE *fp;
unsigned char hdr[54], *line;
long row = ((long) w * 3 + 3) & ~3L;
long size = row * (long) h;
int x, y;
if ((fp = fopen(path, "wb")) == NULL)
return -1;
memset(hdr, 0, sizeof hdr);
hdr[0] = 'B';
hdr[1] = 'M';
Le32Put(hdr + 2, 54L + size);
Le32Put(hdr + 10, 54);
Le32Put(hdr + 14, 40);
Le32Put(hdr + 18, (unsigned long) w);
Le32Put(hdr + 22, (unsigned long) h);
hdr[26] = 1; /* planes */
hdr[28] = 24; /* bits per pixel */
Le32Put(hdr + 34, (unsigned long) size);
Le32Put(hdr + 38, 2835); /* 72 dpi, as the original writes it */
Le32Put(hdr + 42, 2835);
if (fwrite(hdr, 1, sizeof hdr, fp) != sizeof hdr) {
fclose(fp);
return -1;
}
if ((line = malloc((size_t) row)) == NULL) {
fclose(fp);
return -1;
}
for (y = h - 1; y >= 0; y--) {
const unsigned char *s = rgb + (size_t) y * (size_t) w * 3;
memset(line, 0, (size_t) row);
for (x = 0; x < w; x++) {
line[x * 3] = s[x * 3 + 2]; /* BMP is BGR */
line[x * 3 + 1] = s[x * 3 + 1];
line[x * 3 + 2] = s[x * 3];
}
if (fwrite(line, 1, (size_t) row, fp) != (size_t) row) {
free(line);
fclose(fp);
return -1;
}
}
free(line);
return fclose(fp) == 0 ? 0 : -1;
}
int
McdeThumbWrite(const char *path, const unsigned char *rgb, int w, int h)
{
if (path == NULL || rgb == NULL || w <= 0 || h <= 0 ||
w > MAX_WIDTH || h > MAX_HEIGHT ||
(long) w * (long) h > MAX_PIXELS)
return -1;
if (SameEnding(path, "png"))
return WritePng(path, rgb, w, h);
if (SameEnding(path, "bmp"))
return WriteBmp(path, rgb, w, h);
return -1;
}
+99
View File
@@ -308,6 +308,104 @@ TestFormats(const char *dir)
McdeThumbFree(&image);
}
/*
* A picture in which every pixel is unique: red = x * 8 + 8, green = y * 8 +
* 8, blue = 128. A writer that flips the rows, swaps the channels or drops
* the alpha cannot pass this.
*/
static void
Ramp(unsigned char *rgb, int w, int h)
{
int x, y;
for (y = 0; y < h; y++)
for (x = 0; x < w; x++) {
unsigned char *p = rgb + ((size_t) y * w + x) * 3;
p[0] = (unsigned char) (x * 8 + 8);
p[1] = (unsigned char) (y * 8 + 8);
p[2] = 128;
}
}
/*
* The writer: PNG and BMP are written from one RGB buffer and read back with
* the reader above. Every pixel has to come back unchanged, and an ending
* the writer does not know has to be refused -- not written as something else.
*/
static void
TestWrite(const char *dir)
{
unsigned char rgb[13 * 7 * 3];
char path[PATH_MAX];
McdeThumbImage image;
int x, y, bad;
Ramp(rgb, 13, 7);
snprintf(path, sizeof path, "%s/write.png", dir);
CHECK(McdeThumbWrite(path, rgb, 13, 7) == 0);
memset(&image, 0, sizeof image);
CHECK(McdeThumbRead(path, 0, 0, &image) == 0);
CHECK(image.width == 13 && image.height == 7);
if (image.width == 13 && image.height == 7) {
for (bad = 0, y = 0; y < 7; y++)
for (x = 0; x < 13; x++) {
const unsigned char *p = image.rgb +
((size_t) y * 13 + x) * 3;
const unsigned char *q = rgb +
((size_t) y * 13 + x) * 3;
if (p[0] != q[0] || p[1] != q[1] ||
p[2] != q[2])
bad++;
}
if (bad != 0)
fprintf(stderr, "FAIL line %d: png round trip, "
"%d of 91 pixels differ\n", __LINE__, bad);
CHECK(bad == 0);
}
McdeThumbFree(&image);
snprintf(path, sizeof path, "%s/write.bmp", dir);
CHECK(McdeThumbWrite(path, rgb, 13, 7) == 0);
memset(&image, 0, sizeof image);
CHECK(McdeThumbRead(path, 0, 0, &image) == 0);
CHECK(image.width == 13 && image.height == 7);
if (image.width == 13 && image.height == 7) {
for (bad = 0, y = 0; y < 7; y++)
for (x = 0; x < 13; x++) {
const unsigned char *p = image.rgb +
((size_t) y * 13 + x) * 3;
const unsigned char *q = rgb +
((size_t) y * 13 + x) * 3;
if (p[0] != q[0] || p[1] != q[1] ||
p[2] != q[2])
bad++;
}
if (bad != 0)
fprintf(stderr, "FAIL line %d: bmp round trip, "
"%d of 91 pixels differ\n", __LINE__, bad);
CHECK(bad == 0);
}
McdeThumbFree(&image);
/* the ending decides, in either case; an unknown one is refused */
snprintf(path, sizeof path, "%s/write.PNG", dir);
CHECK(McdeThumbWrite(path, rgb, 13, 7) == 0);
CHECK(McdeThumbRead(path, 0, 0, &image) == 0);
McdeThumbFree(&image);
snprintf(path, sizeof path, "%s/write.tiff", dir);
CHECK(McdeThumbWrite(path, rgb, 13, 7) != 0);
snprintf(path, sizeof path, "%s/noending", dir);
CHECK(McdeThumbWrite(path, rgb, 13, 7) != 0);
/* and a picture of nothing is not written at all */
snprintf(path, sizeof path, "%s/write2.png", dir);
CHECK(McdeThumbWrite(path, rgb, 0, 7) != 0);
CHECK(access(path, F_OK) != 0);
}
static void
TestScale(void)
{
@@ -454,6 +552,7 @@ main(void)
return 1;
}
TestFormats(dir);
TestWrite(dir);
TestScale();
TestLimits(dir);
TestCache(dir);
+1499 -37
View File
File diff suppressed because it is too large. Load diff
+1
View File
@@ -7,3 +7,4 @@ $set 1
3 Doc: %dK/%dK
4 The picture could not be opened.
5 The picture could not be saved.
6 Tool Options
@@ -7,3 +7,4 @@ $set 1
3 Dok: %dK/%dK
4 Das Bild konnte nicht geöffnet werden.
5 Das Bild konnte nicht gespeichert werden.
6 Werkzeugoptionen