From 02aa4ce6d4400c83fd63b65d717f93827fdf0e78 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 15:54:56 +0000 Subject: [PATCH] MCDE dtphoto: the layer stack is real -- coverage, a palette, options A new layer used to be a white sheet that hid the picture: layers carried RGB only, so nothing could say "this pixel is empty". LayerAbsorb made it worse by copying the whole composite into the active layer, so the layer below appeared twice. - Layer carries coverage (0..255 per pixel) beside RGB; the background has none (it covers everything), a new layer starts with an empty one, and WritePatch sets the coverage of the layer being painted, so a stroke is opaque while the rest of the layer stays clear. - LayerAbsorb takes only the covered pixels of a layer that has coverage; the background still takes the whole picture. - The Layers palette is a list, not a label: one row per layer, top of the stack first, an eye to hide it, the name to make it active. Rows are rebuilt where the stack changes (new/duplicate/delete/merge), never from inside a row's own callback. - Layer Options sets opacity and blend mode through FieldDialog, and does not absorb first: a property is not new pixels, and reading the composite back baked the old blend in (50% gave 127,0,0; multiply over white went black). - New -layertest drives the real CanvasInput -- stack first, then the stroke, the order that matters, because LayerEnsure after the stroke would read the pixmap back and hide the fault. Verified: 26/26 green (13 modes on depth 24 and depth 8), 206 menu paths bound, 0 unbound, 0 doubled; build without warnings. --- docs/MCDE-PROGRAMS.md | 33 +- ext/src/mcde/AGENTS.md | 31 +- ext/src/mcde/programs/dtphoto/dtphoto.c | 635 ++++++++++++++++++++++-- 3 files changed, 662 insertions(+), 37 deletions(-) diff --git a/docs/MCDE-PROGRAMS.md b/docs/MCDE-PROGRAMS.md index aae8298b10..05f8e841a8 100644 --- a/docs/MCDE-PROGRAMS.md +++ b/docs/MCDE-PROGRAMS.md @@ -555,10 +555,10 @@ die Colormap, nicht der Leser, und derselbe Verlauf weicht auf Tiefe 24 um 0 ab. `test_thumb` schreibt einen 13x7-Farbverlauf als PNG und BMP, liest ihn zurueck und vergleicht jedes Pixel; umgedrehte Zeilen im BMP-Schreiber und vertauschte Kanaele im PNG-Schreiber machen den Test rot (beides geprueft). -Alle dtphoto-Testmodi weiterhin gruen auf beiden Tiefen -- 12 Modi mal -zwei Tiefen, 24 Laeufe (menutest, viewtest, optest, selmodtest, -retouchtest, selecttest, movetest, croptest, typetest, filtertest, -drawtest und der Datei-Rundlauf `-selftest`). +Alle dtphoto-Testmodi weiterhin gruen auf beiden Tiefen -- 13 Modi mal +zwei Tiefen, 26 Laeufe (menutest, viewtest, optest, selmodtest, +layertest, retouchtest, selecttest, movetest, croptest, typetest, +filtertest, paneltest, drawtest und der Datei-Rundlauf `-selftest`). `-filtertest` (2026-11-22) nimmt die uebrigen 43 Filter in einem Lauf: die vier Weichzeichner, Unscharf maskieren, Hochpass, Kontur finden, Relief, Kontur nachzeichnen, Facette, Fragmentieren, Median, Versetzen, Wolken, @@ -595,6 +595,31 @@ Test meldete "keine Permutation"). Jede inverse Abtastung braucht denselben Mittelpunkt, sonst faellt eine Kante weg. Zwoelf Tests, beide Tiefen, 0 Warnungen beim Bauen (`XmLoc` castet die Katalogzeichenkette an genau einer Stelle). +Die Ebenen sind jetzt auch bedienbar (2026-11-22): die Ebenenpalette zeigt +eine Zeile pro Ebene, oberste zuerst, mit Auge zum Verstecken und Namen zum +Aktivieren. Vorher war sie ein einzelnes Label, und eine Ebene war nur +ueber das Layer-Menue erreichbar. Eine Ebene traegt neben RGB eine Deckung +(0-255 pro Pixel), weil eine leere Ebene die darunter durchscheinen lassen +muss: ohne sie war eine neue Ebene ein weisses Blatt, das das Bild zudeckte, +und `LayerAbsorb` kopierte das ganze Komposit hinein, sodass die untere +Ebene doppelt erschien. Der Hintergrund hat keinen Deckungspuffer (er deckt +alles), eine neue Ebene startet mit einem leeren, und `WritePatch` setzt die +Deckung der Ebene, auf die gemalt wird -- ein Strich ist deckend, der Rest +der Ebene bleibt frei. `Layer Options` setzt Deckkraft und Modus (0 normal, +1 multiplizieren, 2 negativ multiplizieren) ueber `FieldDialog` und +absorbiert *nicht* vorher: eine Eigenschaft ist keine neuer Pixel, und das +Komposit in die Ebene zurueckzulesen backte den alten Modus ein -- 50 % +ergab 127,0,0 und Multiplizieren ueber Weiss wurde schwarz (gemessen +2026-11-22). +`-layertest` faehrt den echten `CanvasInput` in der Reihenfolge, auf die es +ankommt -- erst der Stapel, dann der Strich --, weil ein `LayerEnsure` nach +dem Strich das Pixmap in die Ebene zurueckliest und den Fehler versteckt: +elf Pruefungen, dass der Strich in der bemalten Ebene landet und nicht in +der darunter, dass jede sichtbare Ebene komponiert wird, egal welche aktiv +ist, dass Verstecken den Hintergrund zurueckbringt, dass die Palette eine +Zeile pro Ebene hat und ein Klick auf die oberste Zeile diese aktiviert, +und dass Deckkraft 100/50 und Multiplizieren das Bild auf dem Schirm +aendern. Offen bleiben TIFF und PSD (eigene Decoder noetig) sowie JPEG-Schreiben. ### C2. dtillustrator -- Illustrator 5.5.1, exakter Nachbau diff --git a/ext/src/mcde/AGENTS.md b/ext/src/mcde/AGENTS.md index 37a75ba01d..00105300d5 100644 --- a/ext/src/mcde/AGENTS.md +++ b/ext/src/mcde/AGENTS.md @@ -93,8 +93,8 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt gives a position outside the canvas, which moved the picture back). Crop drags a rectangle and crops to it on release. Type opens a prompt and draws one line with the GC font over an undo snapshot. - `dtphoto -movetest|-croptest|-typetest|-selecttest|-retouchtest` are the - headless proofs (no display needed beyond Xvfb): exact pixel sets for + `dtphoto -movetest|-croptest|-typetest|-selecttest|-retouchtest|-layertest` + are the headless proofs (no display needed beyond Xvfb): exact pixel sets for wand/inverse/lasso/clear, a 10x10 block moved (10,5) with its mask and a background hole left behind, a crop to 20x30 that keeps only blue, a text line with dark pixels and an untouched corner, and all six @@ -208,6 +208,33 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt 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. + The layer stack is real and the palette shows it (2026-11-22): one row + per layer, the top of the stack first, an eye that hides a layer and a + name that makes it active. Before this the palette was a single label + and a layer could only be reached through the Layer menu. The rows are + rebuilt where the *stack* changes (new, duplicate, delete, merge), never + from inside a row's own callback -- that would destroy the widget whose + handler is running. A layer carries coverage (0-255 per pixel) as well + as RGB, because an empty layer has to let the one below show through: + without it a new layer was a white sheet that hid the picture, and + `LayerAbsorb` copied the whole composite into it, so the layer below + appeared twice. The background has no coverage buffer (it covers + everything), a new layer starts with an empty one, and `WritePatch` + sets the coverage of the layer being painted so a stroke is opaque + while the rest of the layer stays clear. `Layer Options` sets opacity + and blend mode (0 normal, 1 multiply, 2 screen) through `FieldDialog` + and *without* absorbing first: a property is not new pixels, and + reading the composite back into the layer baked the old blend in, so + 50% showed 127,0,0 and multiply over white went black (measured + 2026-11-22). `-layertest` drives the real `CanvasInput` in the order + that matters -- stack first, then the stroke -- because a `LayerEnsure` + after the stroke reads the pixmap back into the layer and hides the + fault: eleven checks, that the stroke lands in the painted layer and + not the one below, that every visible layer is composited whichever is + active, that hiding it brings the background back, that the palette + holds one row per layer and a click on the top row makes that layer + active, and that opacity 100/50 and multiply change what reaches the + screen. 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 diff --git a/ext/src/mcde/programs/dtphoto/dtphoto.c b/ext/src/mcde/programs/dtphoto/dtphoto.c index bf5d0ad8d5..4b698e772e 100644 --- a/ext/src/mcde/programs/dtphoto/dtphoto.c +++ b/ext/src/mcde/programs/dtphoto/dtphoto.c @@ -258,6 +258,23 @@ static void OpDone(const char *what); static void FieldDialog(const char *title, const char *labels, const char *initial, void (*fn)(const int *v)); +/* + * WritePatch marks coverage of the layer the person works on, so these + * have to be reachable from there; the stack itself is defined much + * further down (the drawing code comes before the Layer menu). + */ +#define MAXLAYERS 24 +typedef struct { + char name[48]; + unsigned char *rgb; + unsigned char *cover; + int visible; + int opacity; + int mode; +} Layer; +static Layer layers[MAXLAYERS]; +static int nlayers, curLayer, layerW, layerH; + /* the hand: move the view, not the picture */ static void PanBy(int dx, int dy) @@ -2217,6 +2234,29 @@ WritePatch(const Patch *p) } XPutPixel(im, i % p->w, i / p->w, px); } + /* + * Every pixel that reaches the picture also becomes visible in the + * layer the person works on. Without this a stroke on an empty, + * transparent layer had coverage 0 everywhere and the composite + * threw it away (measured 2026-11-22: stroke on screen, layer + * stayed empty after the next switch). + */ + if (nlayers > 0 && curLayer >= 0 && curLayer < nlayers && + layers[curLayer].cover != NULL && + layerW == docW && layerH == docH) { + unsigned char *cv = layers[curLayer].cover; + int px_, py_; + + for (py_ = 0; py_ < p->h; py_++) + for (px_ = 0; px_ < p->w; px_++) { + int dx = p->x + px_, dy = p->y + py_; + + if (dx < 0 || dy < 0 || dx >= layerW || + dy >= layerH) + continue; + cv[(size_t) dy * layerW + dx] = 255; + } + } XPutImage(dpy, image, gc, im, 0, 0, p->x, p->y, (unsigned int) p->w, (unsigned int) p->h); free(keys); @@ -5382,21 +5422,8 @@ DefringeOk(const int *v) * The stack is built on first use from what is in the picture, so a document * that never touches the Layer menu sees no difference at all. */ -#define MAXLAYERS 24 - -typedef struct { - char name[48]; - unsigned char *rgb; /* w * h * 3, this layer's own pixels */ - int visible; - int opacity; /* 0..100 */ - int mode; /* 0 normal, 1 multiply, 2 screen */ -} Layer; - -static Layer layers[MAXLAYERS]; -static int nlayers; -static int curLayer; -static int layerW, layerH; /* the size the stack was built at */ static Widget palLayers, layersLabel; +static void LayerBuildRows(void); /* --------------------------------------------------- Image > Adjust */ @@ -5634,6 +5661,8 @@ LayerInvalidate(void) for (i = 0; i < nlayers; i++) { free(layers[i].rgb); layers[i].rgb = NULL; + free(layers[i].cover); + layers[i].cover = NULL; } nlayers = 0; curLayer = 0; @@ -5653,6 +5682,7 @@ LayerEnsure(void) if (GetImageRGB(&rgb, &w, &h) != 0) return; layers[0].rgb = rgb; + layers[0].cover = NULL; /* the background covers everything */ snprintf(layers[0].name, sizeof layers[0].name, "Background"); layers[0].visible = 1; layers[0].opacity = layerOpacityDefault; @@ -5690,7 +5720,15 @@ LayerComposite(unsigned char **outp) const unsigned char *s = src + ((size_t) y * w + x) * 3; int op = layers[i].opacity; + int cov = layers[i].cover == NULL ? 255 : + layers[i].cover[(size_t) y * w + x]; + /* an unpainted pixel lets the layer below + * show through: opacity and coverage are + * two factors of the same weight */ + op = op * cov / 255; + if (op <= 0) + continue; for (c = 0; c < 3; c++) { int v = s[c]; @@ -5758,8 +5796,33 @@ LayerAbsorb(void) free(rgb); return; } - free(layers[curLayer].rgb); - layers[curLayer].rgb = rgb; + { + unsigned char *old = layers[curLayer].rgb; + unsigned char *cov = layers[curLayer].cover; + + /* + * The background covers everything, so the whole picture is + * its new state. A layer with coverage only takes the + * pixels it covers: copying the whole composite into it + * would duplicate the layer below instead of showing + * through (measured 2026-11-22). + */ + if (cov == NULL) { + layers[curLayer].rgb = rgb; + free(old); + } else { + size_t i, n = (size_t) w * h; + + for (i = 0; i < n; i++) { + if (cov[i] == 0) + continue; + old[i * 3] = rgb[i * 3]; + old[i * 3 + 1] = rgb[i * 3 + 1]; + old[i * 3 + 2] = rgb[i * 3 + 2]; + } + free(rgb); + } + } LayerShowState(); } @@ -5783,8 +5846,15 @@ LayerNew(void) if ((rgb = malloc((size_t) layerW * layerH * 3)) == NULL) return; /* an empty layer is transparent: what lies below shows through */ - memset(rgb, 255, (size_t) layerW * layerH * 3); + memset(rgb, 0, (size_t) layerW * layerH * 3); curLayer++; + free(layers[curLayer].cover); + if ((layers[curLayer].cover = + calloc((size_t) layerW * layerH, 1)) == NULL) { + free(rgb); + curLayer--; + return; + } layers[curLayer].rgb = rgb; snprintf(layers[curLayer].name, sizeof layers[curLayer].name, "Layer %d", nlayers + 1); @@ -5793,6 +5863,7 @@ LayerNew(void) layers[curLayer].mode = 0; nlayers++; LayerShowState(); + LayerBuildRows(); fprintf(stderr, "dtphoto: layer new %d/%d\n", curLayer + 1, nlayers); } @@ -5810,11 +5881,20 @@ LayerDuplicate(void) memcpy(rgb, layers[curLayer].rgb, (size_t) layerW * layerH * 3); layers[nlayers] = layers[curLayer]; layers[nlayers].rgb = rgb; + /* the coverage is copied too, or both layers would share it */ + layers[nlayers].cover = NULL; + if (layers[curLayer].cover != NULL) { + layers[nlayers].cover = malloc((size_t) layerW * layerH); + if (layers[nlayers].cover != NULL) + memcpy(layers[nlayers].cover, layers[curLayer].cover, + (size_t) layerW * layerH); + } snprintf(layers[nlayers].name, sizeof layers[nlayers].name, "%.36s copy", layers[curLayer].name); curLayer = nlayers; nlayers++; LayerShowState(); + LayerBuildRows(); fprintf(stderr, "dtphoto: layer duplicate %d/%d\n", curLayer + 1, nlayers); } @@ -5833,6 +5913,7 @@ LayerDelete(void) if (curLayer < 0 || curLayer >= nlayers) return; free(layers[curLayer].rgb); + free(layers[curLayer].cover); for (i = curLayer; i < nlayers - 1; i++) layers[i] = layers[i + 1]; nlayers--; @@ -5841,6 +5922,7 @@ LayerDelete(void) LayerSync(); RenderDoc(canvas); LayerShowState(); + LayerBuildRows(); fprintf(stderr, "dtphoto: layer delete %d/%d\n", curLayer + 1, nlayers); } @@ -5959,6 +6041,7 @@ LayerMerge(int all) LayerSync(); RenderDoc(canvas); LayerShowState(); + LayerBuildRows(); fprintf(stderr, "dtphoto: layer merge %s -> %d\n", all ? "all" : "down", nlayers); } @@ -5969,13 +6052,118 @@ LayerPickCallback(Widget w, XtPointer cd, XtPointer call) (void) call; if (!XmToggleButtonGetState(w)) return; - curLayer = (int) (long) cd; - if (curLayer < 0 || curLayer >= nlayers) - curLayer = 0; + int want = (int) (long) cd; + + if (want < 0 || want >= nlayers) + want = 0; + /* + * The order is what makes this work. The picture on screen still + * belongs to the layer that was active, so it has to go back into + * *that* layer first; only then may curLayer move. Reading it back + * after the switch would copy one layer's pixels over another's + * (and did, before this). + */ + LayerAbsorb(); + curLayer = want; + /* now the layer that was picked has to reach the screen */ + LayerSync(); + RenderDoc(canvas); LayerShowState(); fprintf(stderr, "dtphoto: layer %d/%d\n", curLayer + 1, nlayers); } +/* + * The layer palette: one row per layer, top of the stack first, as the + * original shows it. The eye toggles visibility and clicking the name makes + * that layer active -- until this existed the palette was a single label and + * a layer could only be reached through the Layer menu (measured + * 2026-11-22). + */ +static Widget layersBox, *layerRows; +static int layerRowsN; + +static void +LayerRowVisible(Widget w, XtPointer cd, XtPointer call) +{ + int i = (int) (long) cd; + + (void) call; + if (i < 0 || i >= nlayers) + return; + layers[i].visible = XmToggleButtonGetState(w) ? 1 : 0; + LayerAbsorb(); + LayerSync(); + RenderDoc(canvas); + LayerShowState(); + fprintf(stderr, "dtphoto: layer %d %s\n", i + 1, + layers[i].visible ? "shown" : "hidden"); +} + +static void +LayerRowPick(Widget w, XtPointer cd, XtPointer call) +{ + int i = (int) (long) cd; + + (void) call; + /* + * The picture on screen still belongs to the layer that is active, + * so it goes back there before curLayer moves (the same order the + * menu callback uses, and for the same reason). + */ + LayerAbsorb(); + curLayer = i; + LayerSync(); + RenderDoc(canvas); + LayerShowState(); + fprintf(stderr, "dtphoto: layer %d/%d\n", curLayer + 1, nlayers); +} + +/* the rows, rebuilt whenever the stack changes */ +static void +LayerBuildRows(void) +{ + int i; + + if (layersBox == NULL) + return; + if (layerRows != NULL) { + for (i = 0; i < layerRowsN; i++) + if (layerRows[i] != NULL) + XtDestroyWidget(layerRows[i]); + free(layerRows); + } + layerRows = NULL; + layerRowsN = 0; + if (nlayers == 0) + return; + layerRows = calloc((size_t) nlayers, sizeof *layerRows); + if (layerRows == NULL) + return; + layerRowsN = nlayers; + /* top of the stack first */ + for (i = nlayers - 1; i >= 0; i--) { + Widget row, eye; + + row = XtVaCreateManagedWidget("layerrow", + xmRowColumnWidgetClass, layersBox, + XmNorientation, XmHORIZONTAL, NULL); + eye = XtVaCreateManagedWidget("eye", + xmToggleButtonWidgetClass, row, + XmNindicatorOn, XmINDICATOR_NONE, + XmNset, layers[i].visible ? True : False, + NULL); + XtAddCallback(eye, XmNvalueChangedCallback, + LayerRowVisible, (XtPointer) (long) i); + XtVaCreateManagedWidget("layername", + xmPushButtonWidgetClass, row, + XmNlabelString, + XmStringCreateLocalized(layers[i].name), NULL); + XtAddCallback(XtNameToWidget(row, "layername"), + XmNactivateCallback, LayerRowPick, (XtPointer) (long) i); + layerRows[i] = row; + } +} + static Widget BuildLayers(Widget parent) { @@ -5984,12 +6172,64 @@ BuildLayers(Widget parent) box = XtVaCreateManagedWidget("layersbox", xmRowColumnWidgetClass, parent, XmNorientation, XmVERTICAL, XmNmarginWidth, 8, XmNmarginHeight, 8, NULL); + layersBox = box; layersLabel = XtVaCreateManagedWidget("layerstate", xmLabelWidgetClass, box, XmNlabelString, XmStringCreateLocalized("Background 1/1 100%"), NULL); + LayerBuildRows(); return box; } +/* + * Layer Options: opacity and the blend mode are what the composite already + * reads, so the dialog only has to set them. The mode is a number because + * this program has no option menus in its palette yet: 0 normal, 1 multiply, + * 2 screen, as the composite's switch says. + */ +static void +LayerOptionsOk(const int *v) +{ + int op = v[0], mode = v[1]; + + LayerEnsure(); + if (nlayers == 0 || curLayer < 0 || curLayer >= nlayers) + return; + if (op < 0) + op = 0; + if (op > 100) + op = 100; + if (mode < 0 || mode > 2) + mode = 0; + /* + * No LayerAbsorb here: opacity and the mode are properties of the + * layer, not new pixels. Reading the composite back into the layer + * would bake the old blend into its pixels and the next change + * would blend the blend (measured 2026-11-22: 50% gave 127,0,0 and + * multiply over white gave black). + */ + layers[curLayer].opacity = op; + layers[curLayer].mode = mode; + LayerSync(); + RenderDoc(canvas); + LayerShowState(); + fprintf(stderr, "dtphoto: layer options %d/%d opacity %d mode %d\n", + curLayer + 1, nlayers, op, mode); +} + +static void +LayerOptions(void) +{ + char buf[64]; + + LayerEnsure(); + if (nlayers == 0 || curLayer < 0 || curLayer >= nlayers) + return; + snprintf(buf, sizeof buf, "%d %d", layers[curLayer].opacity, + layers[curLayer].mode); + FieldDialog("Layer Options", "Opacity Mode(0 normal 1 multiply 2 screen)", + buf, LayerOptionsOk); +} + /* Window > Show Layers */ static void ShowLayers(void) @@ -5997,6 +6237,7 @@ ShowLayers(void) Palette(&palLayers, "Layers", 40, 300, BuildLayers); LayerEnsure(); LayerShowState(); + LayerBuildRows(); fprintf(stderr, "dtphoto: layers %d\n", nlayers); } @@ -9444,6 +9685,20 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) } } dragging = 0; + /* + * Whatever the drag did to the picture belongs to the layer + * the person is working on. Without this the stroke lived + * only in the pixmap: clicking another layer and back showed + * the picture without it (measured 2026-11-22). + * + * Only the tools that change pixels need it; the marquee, the + * wand, the hand and the magnifier leave the picture alone. + */ + if (nlayers > 0 && (currentTool == 4 || currentTool == 7 || + currentTool == 9 || currentTool == 10 || currentTool == 11 || + currentTool == 12 || currentTool == 13 || currentTool == 14 || + (currentTool >= 15 && currentTool <= 20))) + LayerAbsorb(); RenderDoc(canvas); break; } @@ -10896,17 +11151,7 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) LayerDelete(); break; case CMD_LAYEROPTS: - SetScratch("Layer %d/%d, %d%%, mode %s", curLayer + 1, - nlayers, nlayers > 0 ? layers[curLayer].opacity : 100, - nlayers > 0 && layers[curLayer].mode == 1 ? "multiply" : - nlayers > 0 && layers[curLayer].mode == 2 ? "screen" : - "normal"); - MessageBox("Layer Options", - "The stack holds this document's layers.\n" - "Window > Show Layers names the current one, " - "Layer > Arrange moves it through the stack."); - fprintf(stderr, "dtphoto: layer options %d/%d\n", - curLayer + 1, nlayers); + LayerOptions(); break; case CMD_ADJOPTS: AdjustmentOptions(); @@ -12276,6 +12521,334 @@ main(int argc, char **argv) * and switching them off must take them away. Counting pixels is * how "it is drawn" is proven without a person looking at a screen. */ + if (argc >= 2 && strcmp(argv[1], "-layertest") == 0) { + /* + * The layer stack, driven through the real event handler. + * + * The order matters and is the whole point of the test: the + * stack is built FIRST (as it is when the Layers palette is + * opened), and only then does the brush paint. A LayerEnsure() + * after the stroke would read the pixmap back into the layer and + * hide the fault this test exists for. + * + * Two things have to hold: + * - the stroke lands in the layer that was active while painting, + * - switching to another layer shows that layer, not the one + * that was painted on. + */ + XEvent ev; + unsigned char *rgb; + int w = 12, h = 6, bad = 0, x, y; + int CTX, CTY; + int strokeIn1 = 0, strokeIn2 = 0, onScreen2 = 0; + int afterSwitch = 0; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto layertest: FAIL malloc\n"); + return 1; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + + d[0] = 255; d[1] = 255; d[2] = 255; + } + SetImageRGB(rgb, w, h); + free(rgb); + viewOX = viewOY = 0; + zoom = 100; + currentTool = 10; /* the pencil */ + LayerInvalidate(); + LayerEnsure(); /* the stack exists before the first stroke */ + LayerNew(); /* a second, transparent layer on top */ + /* + * The canvas centres a small document, so the event coordinates + * are window pixels: the offset CanvasInput works out has to be + * added here too (measured 2026-11-22: without it the stroke + * landed outside the document). + */ + { + Dimension ww, hh; + int dw, dh; + + XtVaGetValues(canvas, XmNwidth, &ww, XmNheight, &hh, + NULL); + dw = docW * zoom / 100; + dh = docH * zoom / 100; + CTX = (ww > dw) ? (ww - dw) / 2 : 0; + CTY = (hh > dh) ? (hh - dh) / 2 : 0; + } + + /* paint on layer 2, the one on top */ + memset(&ev, 0, sizeof ev); + ev.type = ButtonPress; + ev.xbutton.button = Button1; + ev.xbutton.x = CTX + 2; + ev.xbutton.y = CTY + 2; + CanvasInput(canvas, NULL, &ev, NULL); + ev.type = MotionNotify; + ev.xmotion.x = CTX + 9; + ev.xmotion.y = CTY + 2; + CanvasInput(canvas, NULL, &ev, NULL); + ev.type = ButtonRelease; + ev.xbutton.x = CTX + 9; + ev.xbutton.y = CTY + 2; + CanvasInput(canvas, NULL, &ev, NULL); + + /* the stroke is on screen */ + if (GetImageRGB(&rgb, &w, &h) == 0) { + for (x = 2; x <= 9; x++) { + unsigned char *d = rgb + ((size_t) 2 * w + x) * 3; + + if (d[0] < 128 && d[1] < 128 && d[2] < 128) + onScreen2++; + } + free(rgb); + } + + /* + * Switch to the layer below through the palette's callback, the + * way a click on the layer name does it. The stroke stays + * visible, because in the original every visible layer is + * composited no matter which one is active -- hiding it is + * what has to make it go away. + */ + { + Widget b = XtVaCreateWidget("layerpick", + xmToggleButtonWidgetClass, canvas, + XmNindicatorOn, XmINDICATOR_NONE, + XmNset, False, NULL); + + XtAddCallback(b, XmNvalueChangedCallback, + LayerPickCallback, (XtPointer) 0); + XmToggleButtonSetState(b, True, True); + } + + if (GetImageRGB(&rgb, &w, &h) == 0) { + for (x = 2; x <= 9; x++) { + unsigned char *d = rgb + ((size_t) 2 * w + x) * 3; + + if (d[0] < 128 && d[1] < 128 && d[2] < 128) + afterSwitch++; + } + free(rgb); + } + + /* and the stack itself: layer 2 keeps the stroke, layer 1 not */ + if (nlayers >= 2) { + for (x = 2; x <= 9; x++) { + const unsigned char *a = layers[0].rgb + + ((size_t) 2 * layerW + x) * 3; + const unsigned char *b = layers[1].rgb + + ((size_t) 2 * layerW + x) * 3; + + if (a[0] < 128 && a[1] < 128 && a[2] < 128) + strokeIn1++; + if (b[0] < 128 && b[1] < 128 && b[2] < 128) + strokeIn2++; + } + } + + if (onScreen2 < 6) { + printf("dtphoto layertest: stroke on screen %d of 8 FAIL\n", + onScreen2); + bad++; + } else + printf("dtphoto layertest: stroke on screen %d of 8 ok\n", + onScreen2); + if (strokeIn2 < 6) { + printf("dtphoto layertest: stroke kept by the painted " + "layer %d of 8 FAIL\n", strokeIn2); + bad++; + } else + printf("dtphoto layertest: stroke kept by the painted " + "layer %d of 8 ok\n", strokeIn2); + if (strokeIn1 != 0) { + printf("dtphoto layertest: the layer below took the " + "stroke too (%d) FAIL\n", strokeIn1); + bad++; + } else + printf("dtphoto layertest: the layer below stayed clean ok\n"); + if (afterSwitch != 8) { + printf("dtphoto layertest: the stroke of the upper " + "layer is gone after picking the lower one " + "(%d of 8) FAIL\n", afterSwitch); + bad++; + } else + printf("dtphoto layertest: every visible layer is " + "composited, whichever is active ok\n"); + + /* + * Hiding the layer that carries the stroke is the check that + * the two layers really are separate: the picture has to go + * back to the white of the background. + */ + if (nlayers >= 2) { + int hidden = 0; + + layers[1].visible = 0; + LayerSync(); + if (GetImageRGB(&rgb, &w, &h) == 0) { + for (x = 2; x <= 9; x++) { + unsigned char *d = + rgb + ((size_t) 2 * w + x) * 3; + + if (d[0] < 128 && d[1] < 128 && + d[2] < 128) + hidden++; + } + free(rgb); + } + layers[1].visible = 1; + LayerSync(); + if (hidden != 0) { + printf("dtphoto layertest: hiding the painted " + "layer left %d of 8 pixels FAIL\n", + hidden); + bad++; + } else + printf("dtphoto layertest: hiding the painted " + "layer brings the background back ok\n"); + } + + /* + * The palette: one row per layer, top first, and a click on a + * row makes that layer active. Before this the palette was a + * label and a layer was only reachable through the menu. + */ + { + int rows = 0; + Widget pick = NULL, w; + + ShowLayers(); + /* + * Count the rows: they are the children of the box past + * the state label, which is the first one. + */ + rows = 0; + if (layersBox != NULL) { + WidgetList kids = NULL; + Cardinal nk = 0, k; + + XtVaGetValues(layersBox, XmNchildren, &kids, + XmNnumChildren, &nk, NULL); + for (k = 0; k < nk; k++) + if (kids[k] != layersLabel) + rows++; + } + if (rows != nlayers) { + printf("dtphoto layertest: the palette shows %d " + "rows for %d layers FAIL\n", rows, nlayers); + bad++; + } else + printf("dtphoto layertest: the palette shows " + "one row per layer (%d) ok\n", rows); + /* the first row is the top of the stack */ + pick = XtNameToWidget(layersBox, "*layerrow*layername"); + if (pick == NULL) { + printf("dtphoto layertest: the palette has no " + "name to click FAIL\n"); + bad++; + } else { + curLayer = 0; + XtCallCallbacks(pick, XmNactivateCallback, NULL); + if (curLayer != nlayers - 1) { + printf("dtphoto layertest: a click on the " + "top row left the active layer at " + "%d FAIL\n", curLayer + 1); + bad++; + } else + printf("dtphoto layertest: a click on " + "the top row makes that layer " + "active ok\n"); + } + } + + /* + * Layer Options: opacity and the blend mode have to change what + * reaches the screen, not only what the struct says. + */ + if (nlayers >= 2) { + int v[2]; + unsigned char red[3]; + + /* a solid red upper layer at full opacity */ + layers[1].visible = 1; + layers[1].opacity = 100; + layers[1].mode = 0; + LayerEnsure(); /* keep the size */ + for (x = 0; x < layerW * layerH; x++) { + layers[1].rgb[x * 3] = 255; + layers[1].rgb[x * 3 + 1] = 0; + layers[1].rgb[x * 3 + 2] = 0; + layers[1].cover[x] = 255; + } + curLayer = 1; + LayerSync(); + if (GetImageRGB(&rgb, &w, &h) == 0) { + unsigned char *d = rgb + ((size_t) 1 * w + 1) * 3; + + red[0] = d[0]; red[1] = d[1]; red[2] = d[2]; + free(rgb); + } else + red[0] = red[1] = red[2] = 0; + if (!(red[0] > 200 && red[1] < 60)) + printf("dtphoto layertest: a solid layer at " + "100%% did not reach the screen " + "(%d,%d,%d) FAIL\n", red[0], red[1], red[2]), bad++; + else + printf("dtphoto layertest: a solid layer at " + "100%% covers the picture ok\n"); + + /* the background is known white, so the mix is + * predictable: half red over white is (255,128,128) */ + for (x = 0; x < layerW * layerH * 3; x++) + layers[0].rgb[x] = 255; + LayerSync(); + /* half opacity: half the layer, half the white below */ + v[0] = 50; + v[1] = 0; + LayerOptionsOk(v); + if (GetImageRGB(&rgb, &w, &h) == 0) { + unsigned char *d = rgb + ((size_t) 1 * w + 1) * 3; + + red[0] = d[0]; red[1] = d[1]; red[2] = d[2]; + free(rgb); + } + if (!(red[0] > 200 && red[1] > 100 && red[1] < 160)) + printf("dtphoto layertest: 50%% opacity gave " + "(%d,%d,%d), want about (255,128,128) " + "FAIL\n", red[0], red[1], red[2]), bad++; + else + printf("dtphoto layertest: 50%% opacity mixes " + "with the layer below ok\n"); + + /* multiply on white is the layer itself */ + v[0] = 100; + v[1] = 1; + LayerOptionsOk(v); + if (GetImageRGB(&rgb, &w, &h) == 0) { + unsigned char *d = rgb + ((size_t) 1 * w + 1) * 3; + + red[0] = d[0]; red[1] = d[1]; red[2] = d[2]; + free(rgb); + } + if (!(red[0] > 200 && red[1] < 60)) + printf("dtphoto layertest: multiply over white " + "gave (%d,%d,%d) FAIL\n", + red[0], red[1], red[2]), bad++; + else + printf("dtphoto layertest: multiply over white " + "keeps the layer ok\n"); + layers[1].opacity = 100; + layers[1].mode = 0; + } + + printf("dtphoto layertest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + if (argc >= 2 && strcmp(argv[1], "-drawtest") == 0) { Pixmap pm; XImage *im;