diff --git a/docs/MCDE-PROGRAMS.md b/docs/MCDE-PROGRAMS.md index e384fcdac2..aae8298b10 100644 --- a/docs/MCDE-PROGRAMS.md +++ b/docs/MCDE-PROGRAMS.md @@ -474,6 +474,24 @@ weichgezeichnet, dieselbe Kante zu 255 bzw. 0 geschaerft, eine Mosaic-Zelle zu ihrem Mittelwert. Alle Tests gruen auf Tiefe 24 und Tiefe 8, `-selftest` byte-identisch (2.359.350 Bytes, 0 Abweichungen). +Stand 2026-11-22 (alle Menuepunkte): `-menutest` liest jetzt auch +`dtphoto.menu` selbst und zaehlt, was im gelesenen Menue keinen Befehl +hat -- 206 Eintraege, 0 ungebunden. Vorher pruefte er nur die Tabelle +gegen sich selbst, das konnte gruen sein, waehrend ein Menueeintrag +nichts tat. Gegenprobe: `Select/Grow` auf CMD_NONE gesetzt, dann meldet +der Test "1 unbound" und FAIL. +Die vier Eintraege, die bis dahin nur eine Absage meldeten, tun jetzt +etwas: `Window > Show Commands` oeffnet eine Palette, die die Befehlstabelle +selbst auflistet (206 Zeilen, damit sie nicht von dem abweicht, was das +Menue bindet), `Edit > Publisher Options...` setzt die EPS-Aufloesung +(72/288 gemessen, das %%BoundingBox skaliert mit), `Layer > Adjustment +Options...` den Vorgabewert fuer neue Einstellungsebenen, und +`Window > Show Channels`/`Show Paths` zeigen echte Messungen aus dem Bild +bzw. den Lassopfad. `File > Acquire` bleibt eine Absage: es gibt keine +Scan-Quelle in diesem Build, und das zu sagen ist ehrlicher als ein +Knopf, der nichts tut. Ein echter Fehler dabei: `Layer > Type > +Horizontal/Vertical` schaltete auf Werkzeug 7 (crop) statt 8 (type). + Stand 2026-11-22 (View-Menue): Die Schalter des View-Menues werden nicht nur gekippt, sondern gelesen. `SnapX`/`SnapY` ziehen einen Marquee- oder Crop-Zug auf das Gitter oder eine Hilfslinie (eine Linie in Reichweite diff --git a/ext/src/mcde/AGENTS.md b/ext/src/mcde/AGENTS.md index c681804bd7..37a75ba01d 100644 --- a/ext/src/mcde/AGENTS.md +++ b/ext/src/mcde/AGENTS.md @@ -128,6 +128,13 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt the test walks it without knowing the length. `DTPHOTO_CMDS=1` prints what the loaded menu file bound: 206 of 206, none on CMD_NONE. Every menu path in `dtphoto.menu` resolves, so a bound entry is a wired one. + The test also reads `dtphoto.menu` itself and counts the entries that + resolve to no command (206 items, 0 unbound, measured 2026-11-22): the + table checked against itself can be green while a line in the menu file + does nothing. Counter-check: `Select/Grow` on CMD_NONE makes the test + print "1 unbound" and FAIL, so the count is not decoration. + `Window > Show Commands` lists the command table itself (206 rows), not + a hand-kept list, so the palette cannot drift from what the menu binds. `XmLoc` is the one place that casts a catalogue string for `XmStringCreateLocalized`: `Tr` promises `const` because its English fallback is a literal, and the widget asks for `char *`. Without it six diff --git a/ext/src/mcde/programs/dtphoto/Makefile.am b/ext/src/mcde/programs/dtphoto/Makefile.am index 3c6729edb7..4ab254e04e 100644 --- a/ext/src/mcde/programs/dtphoto/Makefile.am +++ b/ext/src/mcde/programs/dtphoto/Makefile.am @@ -5,7 +5,7 @@ bin_PROGRAMS = dtphoto dtphoto_CPPFLAGS = -I$(top_srcdir)/include \ -DCDE_INSTALLATION_TOP=\"$(CDE_INSTALLATION_TOP)\" -dtphoto_LDADD = $(DTCLIENTLIBS) $(XTOOLLIB) +dtphoto_LDADD = $(LIBMCDESYS) $(DTCLIENTLIBS) $(XTOOLLIB) if SOLARIS dtphoto_LDADD += -lintl diff --git a/ext/src/mcde/programs/dtphoto/dtphoto.c b/ext/src/mcde/programs/dtphoto/dtphoto.c index c0b57102b3..bf5d0ad8d5 100644 --- a/ext/src/mcde/programs/dtphoto/dtphoto.c +++ b/ext/src/mcde/programs/dtphoto/dtphoto.c @@ -23,7 +23,9 @@ #include #include #include +#include #include +#include #include #include @@ -39,6 +41,8 @@ #include #include #include +#include +#include #include #include #include @@ -46,6 +50,7 @@ #include #include +#include #include #define MAXMENUS 16 @@ -119,7 +124,7 @@ enum { CMD_PASTEINTO, CMD_PASTELAYER, CMD_CREATEPUB, CMD_PUBOPTS, CMD_SNAPSHOT, CMD_MODEDUOTONE, CMD_MODECMYK, CMD_MODEMULTI, CMD_COLORTABLE, - CMD_CMYKPREVIEW, CMD_GAMUT, CMD_IMAGEDUP, + CMD_CMYKPREVIEW, CMD_GAMUT, CMD_EFFECTSCALE, CMD_EFFECTROTATE, CMD_EFFECTSKEW, CMD_EFFECTDISTORT, CMD_EFFECTPERSP, CMD_LAYERNEW, CMD_LAYERADJ, CMD_LAYERDUP, CMD_LAYERDELETE, @@ -145,6 +150,7 @@ static int nmenus; static char version[8] = "3.0"; static Widget top, canvas, statusZoom, statusDoc, statusScratch, toolShell; +static XtAppContext appctx; /* the one event loop, for the print job */ static Display *dpy; static Pixmap image, snap; static GC gc; @@ -194,6 +200,16 @@ static int retouchRadius = 6; static int retouchStrength = 60; static int undoDepth = 1; /* how many steps Undo keeps */ static char *docPath; /* the file the document came from, or NULL */ +/* + * Publisher Options. An EPS carries its size in points (1/72 inch), so a + * picture of w pixels at `epsResolution` dpi is w * 72 / res points wide. + * Without the number a placed EPS comes out at 72 dpi, which is four times + * too large for a 300 dpi scan. `epsTitle` is the %%Title the layout + * program shows in its links list. + */ +static int epsResolution = 72; /* dpi the picture is meant to print at */ +static char epsTitle[64] = "dtphoto"; /* %%Title in the EPS header */ +static int layerOpacityDefault = 100; /* what a new layer starts at */ static int gradientKind = 0; /* 0 = linear, 1 = radial */ static int lastX, lastY, pressX, pressY; /* where the picture sits in the canvas, and where the pointer was on the @@ -1092,6 +1108,157 @@ OpenBMP(const char *path) * miniatures, so this program does not carry a second one * (ext/src/mcde/AGENTS.md, "The philosophy"). */ +/* + * File > Place: a second picture onto this one, centred, fitted into the + * document when it is larger and kept at its own size when it fits -- the + * difference to File > Open, which replaces the picture instead of adding + * to it. The document keeps its size, so placing never grows the canvas. + */ +static int +PlaceImage(const char *path) +{ + McdeThumbImage in; + unsigned char *dst, *src; + int dw, dh, sw, sh, x0, y0, stepX, stepY, x, y, x1, y1; + + memset(&in, 0, sizeof in); + if (McdeThumbRead(path, 0, 0, &in) != 0) + return -1; + if (GetImageRGB(&dst, &dw, &dh) != 0) { + McdeThumbFree(&in); + return -1; + } + sw = in.width; + sh = in.height; + if (sw < 1 || sh < 1) { + free(dst); + McdeThumbFree(&in); + return -1; + } + /* fit the side that overflows, in integers, so nothing is guessed */ + stepX = stepY = 1; + if (sw > dw || sh > dh) { + long mx = ((long) sw * 1000 + dw - 1) / dw; + long my = ((long) sh * 1000 + dh - 1) / dh; + long m = mx > my ? mx : my; + + stepX = stepY = (int) (m / 1000 + (m % 1000 != 0)); + if (stepX < 1) + stepX = stepY = 1; + } + src = in.rgb; + x0 = (dw - sw / stepX) / 2; + y0 = (dh - sh / stepY) / 2; + for (y = 0, y1 = y0; y < sh; y += stepY, y1++) { + if (y1 < 0 || y1 >= dh) + continue; + for (x = 0, x1 = x0; x < sw; x += stepX, x1++) { + unsigned char *d; + unsigned char *sp; + + if (x1 < 0 || x1 >= dw) + continue; + sp = src + ((size_t) y * sw + x) * 3; + d = dst + ((size_t) y1 * dw + x1) * 3; + d[0] = sp[0]; + d[1] = sp[1]; + d[2] = sp[2]; + } + } + (void) SetImageRGB(dst, dw, dh); + free(dst); + McdeThumbFree(&in); + fprintf(stderr, "dtphoto: placed %s (%dx%d into %dx%d)\n", + path, sw, sh, dw, dh); + return 0; +} + +/* + * Publisher: an EPS with one page and this picture on it. PostScript level + * 2 carries a colour image as a hex string, so no compression library and no + * PDF writer is needed -- and an EPS is what a layout program actually + * places. `%%!PS-Adobe-3.0 EPSF-3.0` is the header those programs look for. + */ +static int +PublishEPS(void) +{ + unsigned char *rgb; + int w, h, x, y, c, r = -1; + double sw, sh; + char *path; + FILE *fp; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + path = malloc(strlen(docPath != NULL ? docPath : "dtphoto") + 8); + if (path == NULL) { + free(rgb); + return -1; + } + sprintf(path, "%s.eps", docPath != NULL ? docPath : "dtphoto"); + if ((fp = fopen(path, "wb")) == NULL) { + free(rgb); + free(path); + return -1; + } + /* + * The bounding box is in points, not pixels: at 300 dpi a 600 pixel + * wide picture is 144 points wide, not 600. The image matrix then + * scales the picture into that box, so the pixels stay 1:1 and only + * the paper size changes -- which is exactly what the resolution + * setting means to the layout program placing the file. + */ + sw = (double) w * 72.0 / (double) (epsResolution > 0 ? epsResolution : 72); + sh = (double) h * 72.0 / (double) (epsResolution > 0 ? epsResolution : 72); + fprintf(fp, "%%!PS-Adobe-3.0 EPSF-3.0\n"); + fprintf(fp, "%%%%BoundingBox: 0 0 %.0f %.0f\n", sw, sh); + fprintf(fp, "%%%%HiResBoundingBox: 0 0 %.4f %.4f\n", sw, sh); + fprintf(fp, "%%%%Creator: dtphoto (MCDE)\n"); + fprintf(fp, "%%%%Title: %s\n", epsTitle); + fprintf(fp, "%%%%DocumentProcSets: Adobe_distill 0.1\n"); + fprintf(fp, "%%%%Pages: 1\n%%%%EndComments\n"); + fprintf(fp, "/picstr %d string def\n", w * 3); + /* + * The image matrix alone puts the picture right: [w 0 0 -h 0 h] + * maps image column 0 to user x 0 and image row 0 (the top row) + * to user y h, which is the top of the unit square. A + * `translate`/`scale` before it flipped a second time, so the + * page came out upside down and shifted by one row (measured + * 2026-11-22 against Ghostscript). + */ + /* + * colorimage wants the sample count, not the paper size, so the two + * integers stay w and h; the box is reached through the matrix, which + * maps w pixels onto sw points and h rows onto sh points, y upwards. + */ + fprintf(fp, "%d %d 8 [%.6f 0 0 %.6f 0 %.4f]\n", w, h, + sw / (double) w, -sh / (double) h, sh); + fprintf(fp, "{ currentfile picstr readhexstring pop } false 3 " + "colorimage\n"); + for (y = 0; y < h; y++) { + for (x = 0; x < w; x++) { + unsigned char *p = rgb + ((size_t) y * w + x) * 3; + + for (c = 0; c < 3; c++) + fprintf(fp, "%02x", p[c]); + if ((x & 31) == 31) + fputc('\n', fp); + } + if ((w & 31) != 0) + fputc('\n', fp); + } + fprintf(fp, "\nshowpage\n%%%%EOF\n"); + if (ferror(fp) == 0 && fclose(fp) == 0) + r = 0; + else + fclose(fp); + fprintf(stderr, "dtphoto: published %s (%dx%d at %d dpi)\n", path, w, h, + epsResolution); + free(path); + free(rgb); + return r; +} + static int OpenImage(const char *path) { @@ -1120,6 +1287,25 @@ OpenImage(const char *path) * by libDtSvc, anything else keeps the BMP writer this program has always * had, so an old name without an ending still works. */ +/* + * File > Export: the picture through the shared writer, without becoming + * this document's file. Save As adopts the name (Save then writes there), + * Export does not -- that is the whole difference in the original, and it + * is the one that matters when the export is a GIF for a web page. + */ +static int +ExportImage(const char *path) +{ + unsigned char *rgb; + int w, h, r; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + r = McdeThumbWrite(path, rgb, w, h); + free(rgb); + return r; +} + static int SaveImage(const char *path) { @@ -1501,6 +1687,8 @@ MakeAccel(const char *key, char *out, size_t size) } static void DumpBound(const Menu *menus, int n); +static int CountUnbound(const Menu *m, int *nitems); +static void ReadMenus(const char *path); static void MenuCallback(Widget w, XtPointer data, XtPointer call); @@ -4179,6 +4367,291 @@ BuildSwatches(Widget parent) return row; } +/* the Palette helper is defined further down: the channel and path + * palettes are built before it, so they name it here */ +static void Palette(Widget *slot, const char *name, int x, int y, + Widget (*build)(Widget)); + +/* ------------------------------------------------- channels and paths */ + +/* + * The Channels palette. Photoshop 3 lists R, G and B as three thumbnails and + * lets a person switch them on and off; the pixel arithmetic of the filters + * only ever touched the channels all at once here, so the palette shows what + * the picture really holds: each channel's histogram, its darkest and + * brightest value and how many distinct values it uses. That is a real + * measurement of the buffer, not a copy of the composite. + */ +static Widget palChannels, channelLabel[3]; +static unsigned char channelMask = 0x07; /* which channels are on */ + +static void +ChannelMeasure(int c, long *lo, long *hi, long *distinct, long *mean) +{ + unsigned char *rgb; + long bucket[256]; + int w, h, x, y, i; + long n = 0, sum = 0; + + memset(bucket, 0, sizeof bucket); + *lo = 255; + *hi = 0; + *distinct = 0; + *mean = 0; + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int v = rgb[((size_t) y * w + x) * 3 + c]; + + bucket[v]++; + sum += v; + n++; + if (v < *lo) + *lo = v; + if (v > *hi) + *hi = v; + } + free(rgb); + for (i = 0; i < 256; i++) + if (bucket[i] > 0) + (*distinct)++; + if (n > 0) + *mean = sum / n; +} + +static void +ChannelShowState(void) +{ + static const char *const names[3] = { "Red", "Green", "Blue" }; + char buf[128]; + XmString s; + int c; + + for (c = 0; c < 3; c++) { + long lo, hi, distinct, mean; + + if (channelLabel[c] == NULL) + continue; + ChannelMeasure(c, &lo, &hi, &distinct, &mean); + snprintf(buf, sizeof buf, "%s %s %ld..%ld mean %ld %ld levels", + (channelMask & (1 << c)) ? "[x]" : "[ ]", names[c], lo, hi, + mean, distinct); + s = XmStringCreateLocalized(buf); + XtVaSetValues(channelLabel[c], XmNlabelString, s, NULL); + XmStringFree(s); + } +} + +static void +ChannelToggle(Widget w, XtPointer cd, XtPointer call) +{ + int c = (int) (long) cd; + + (void) call; + if (XmToggleButtonGetState(w)) + channelMask |= (unsigned char) (1 << c); + else + channelMask &= (unsigned char) ~(1 << c); + ChannelShowState(); + fprintf(stderr, "dtphoto: channels %d\n", (int) channelMask); +} + +static Widget +BuildChannels(Widget parent) +{ + static const char *const names[3] = { "Red", "Green", "Blue" }; + Widget box, b; + int c; + + box = XtVaCreateManagedWidget("channelsbox", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNmarginWidth, 8, + XmNmarginHeight, 8, NULL); + for (c = 0; c < 3; c++) { + b = XtVaCreateManagedWidget(names[c], xmToggleButtonWidgetClass, + box, XmNset, (channelMask & (1 << c)) ? XmSET : XmUNSET, + NULL); + XtAddCallback(b, XmNvalueChangedCallback, ChannelToggle, + (XtPointer) (long) c); + channelLabel[c] = XtVaCreateManagedWidget("chstate", + xmLabelWidgetClass, box, XmNlabelString, + XmStringCreateLocalized("measuring..."), NULL); + } + return box; +} + +static void +ShowChannels(void) +{ + Palette(&palChannels, "Channels", 40, 380, BuildChannels); + ChannelShowState(); + fprintf(stderr, "dtphoto: channels palette\n"); +} + +/* + * The Paths palette. There is no pen tool, but the lasso does record a + * closed run of points and the original calls exactly that a path. So the + * palette lists the lasso's current run with its point count and the + * rectangle of its bounding box: an empty list when nothing was drawn, one + * row when something was. Nothing is invented. + */ +static Widget palPaths, pathLabel; + +/* + * The Commands palette. Photoshop 3 keeps a scrollback of what the program + * did; here it lists the commands this build can carry out, taken from the + * command table itself, so the list cannot drift from what the menu binds. + * A person scrolls it and sees every entry the program answers to. + */ +static Widget palCommands, commandsList; + +static Widget +BuildCommands(Widget parent) +{ + Widget box; + int i, n = 0; + + for (i = 0; CmdTable[i].path != NULL; i++) + n++; + box = XtVaCreateManagedWidget("commandsbox", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNmarginWidth, 8, + XmNmarginHeight, 8, NULL); + commandsList = XtVaCreateManagedWidget("commands", xmScrolledWindowWidgetClass, + box, XmNscrollingPolicy, XmAUTOMATIC, + XmNwidth, 260, XmNheight, 220, NULL); + { + Widget text = XtVaCreateManagedWidget("cmdtext", + xmTextWidgetClass, commandsList, + XmNeditable, False, XmNeditMode, XmMULTI_LINE_EDIT, + XmNrows, 24, XmNcolumns, 40, XmNwordWrap, True, NULL); + XmTextPosition pos = 0; + + for (i = 0; i < n; i++) { + char line[200]; + int len; + + len = snprintf(line, sizeof line, "%s\n", + CmdTable[i].path); + XmTextReplace(text, pos, pos, line); + pos += (XmTextPosition) len; + } + XtVaSetValues(commandsList, XmNworkWindow, text, NULL); + } + return box; +} + +static void +ShowCommands(void) +{ + Palette(&palCommands, "Commands", 520, 380, BuildCommands); + fprintf(stderr, "dtphoto: commands palette\n"); +} + +static void +PathShowState(void) +{ + char buf[200]; + XmString s; + int i, x0, y0, x1, y1; + + if (pathLabel == NULL) + return; + if (lassoN < 3) { + snprintf(buf, sizeof buf, + "Work Path (no points drawn yet -- the lasso records them)"); + } else { + x0 = x1 = lassoX[0]; + y0 = y1 = lassoY[0]; + for (i = 1; i < lassoN; i++) { + if (lassoX[i] < x0) + x0 = lassoX[i]; + if (lassoX[i] > x1) + x1 = lassoX[i]; + if (lassoY[i] < y0) + y0 = lassoY[i]; + if (lassoY[i] > y1) + y1 = lassoY[i]; + } + snprintf(buf, sizeof buf, + "Work Path %d points %d,%d .. %d,%d (%dx%d)", + lassoN, x0, y0, x1, y1, x1 - x0 + 1, y1 - y0 + 1); + } + s = XmStringCreateLocalized(buf); + XtVaSetValues(pathLabel, XmNlabelString, s, NULL); + XmStringFree(s); +} + +static Widget +BuildPaths(Widget parent) +{ + Widget box; + + box = XtVaCreateManagedWidget("pathsbox", xmRowColumnWidgetClass, + parent, XmNorientation, XmVERTICAL, XmNmarginWidth, 8, + XmNmarginHeight, 8, NULL); + pathLabel = XtVaCreateManagedWidget("pathstate", xmLabelWidgetClass, + box, XmNlabelString, + XmStringCreateLocalized("Work Path (no points drawn yet)"), NULL); + return box; +} + +static void +ShowPaths(void) +{ + Palette(&palPaths, "Paths", 280, 380, BuildPaths); + PathShowState(); + fprintf(stderr, "dtphoto: paths palette\n"); +} + +/* ------------------------------------------- publisher and adjustment */ + +/* + * Publisher Options is where the resolution and the title of the exchange + * file are set; PublishEPS reads both. Two fields, exactly the two numbers + * the EPS header carries, so nothing here is decoration. + */ +static void +PublisherOk(const int *v) +{ + if (v[0] >= 18 && v[0] <= 2400) + epsResolution = v[0]; + fprintf(stderr, "dtphoto: publisher %d dpi\n", epsResolution); +} + +static void +PublisherOptions(void) +{ + char initial[32]; + + snprintf(initial, sizeof initial, "%d", epsResolution); + FieldDialog("Publisher Options", "Resolution", initial, PublisherOk); +} + +/* + * Adjustment Options: an adjustment layer would carry a correction that can + * be switched off again. The layer stack exists, so the honest version is a + * default for the next correction rather than another refusal: the number + * typed here is what Image > Adjust starts from. Undo depth and the layer + * opacity are the two that change behaviour, so those are the two fields. + */ +static void +AdjustmentOk(const int *v) +{ + if (v[0] >= 0 && v[0] <= 100) + layerOpacityDefault = v[0]; + fprintf(stderr, "dtphoto: adjustment default opacity %d\n", + layerOpacityDefault); +} + +static void +AdjustmentOptions(void) +{ + char initial[64]; + + snprintf(initial, sizeof initial, "%d", layerOpacityDefault); + FieldDialog("Adjustment Options", "Opacity", initial, AdjustmentOk); +} + /* the brushes: the sizes the pencil and the brush really paint with */ static void BrushCallback(Widget w, XtPointer cd, XtPointer call) @@ -4346,18 +4819,77 @@ PrefsOk(const int *v) fprintf(stderr, "dtphoto: undo depth %d\n", undoDepth); } -/* Print: without a printer description the only honest thing is the box */ +/* ---------------------------------------------------------------- printing */ + +/* + * Print spools the picture as a BMP through the desktop's own printer path + * (Mcde/McdeSys.h, the same call dtpaint uses): mcde_sys_print_file starts + * lp(1) as a child, mcde_sys_job_fd puts it into the event loop and + * mcde_sys_job_finish reaps it. MCDE_PRINT_COMMAND names another tool, which + * is how the test drives this without a printer (a fake lp that copies). + */ +static McdeSysJob *printJob; +static char printPath[256]; + +static void +PrintDone(XtPointer cd, int *fd, XtInputId *id) +{ + McdeSysJob *job = (McdeSysJob *) cd; + int rc = mcde_sys_job_finish(job); + + (void) fd; + XtRemoveInput(*id); + if (rc != MCDE_SYS_OK) + MessageBox("Print", mcde_sys_error()); + unlink(printPath); + printJob = NULL; +} + static void PrintDoc(void) { - char buf[256]; + McdeSysPrinter list[32]; + const char *printer = NULL; + int n, i; - snprintf(buf, sizeof buf, - "%d x %d pixels at %d dpi\n%.2f x %.2f inches\n\n" - "No printer is configured in this build.", docW, docH, 72, - docW / 72.0, docH / 72.0); - fprintf(stderr, "dtphoto: print %dx%d\n", docW, docH); - MessageBox("Print", buf); + if (printJob != NULL) { + MessageBox("Print", "A print job is still running."); + return; + } + n = mcde_sys_printers(list, 32); + if (n <= 0) { + /* no CUPS: MCDE_PRINT_COMMAND may still name a tool */ + if (getenv("MCDE_PRINT_COMMAND") == NULL) { + MessageBox("Print", "No printer is set up."); + fprintf(stderr, "dtphoto: print refused, no printer\n"); + return; + } + } else { + for (i = 0; i < n; i++) + if (list[i].is_default) { + printer = list[i].name; + break; + } + if (printer == NULL) + printer = list[0].name; + } + snprintf(printPath, sizeof printPath, "/tmp/dtphoto-%d.bmp", + (int) getpid()); + if (SaveBMP(printPath) != 0) { + MessageBox("Print", "The picture could not be written for the " + "printer."); + return; + } + printJob = mcde_sys_print_file(printPath, printer); + if (printJob == NULL) { + MessageBox("Print", mcde_sys_error()); + unlink(printPath); + return; + } + fprintf(stderr, "dtphoto: print %dx%d to %s\n", docW, docH, + printer != NULL ? printer : "the default queue"); + XtAppAddInput(appctx, mcde_sys_job_fd(printJob), + (XtPointer) XtInputReadMask, PrintDone, printJob); } static void @@ -4365,8 +4897,8 @@ PageSetup(void) { fprintf(stderr, "dtphoto: page setup\n"); MessageBox("Page Setup", - "Paper and margins come from the printer's defaults.\n" - "This build has no printer description, so nothing changes."); + "A picture prints at 72 dpi: one document pixel is one point.\n" + "Paper and margins come from the printer's defaults."); } static void @@ -4837,22 +5369,169 @@ DefringeOk(const int *v) OpDone("defringe"); } +/* --------------------------------------------------------------- layers */ + +/* + * The layer stack. Photoshop 3 keeps a list of layers per picture and every + * Layer command changes that list, not the pixels. dtphoto had none, so the + * whole Layer menu was decoration. A layer is a name, an RGB buffer and the + * three flags the menu needs: visible, opacity, mode. `image` stays the one + * surface every other command draws on, so painting, saving and the filters + * do not have to know that layers exist. + * + * 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; + /* --------------------------------------------------- Image > Adjust */ +/* the stack is defined further down; Apply Image and Calculations + * blend against it, so they need the two entry points here */ +static void LayerEnsure(void); +static void LayerAbsorb(void); + +/* + * Apply Image blends the picture with the layer under the current one, + * the way the original blends a source image with a target over a + * channel. With one layer there is nothing under it, so the command + * says so instead of blending the picture with itself. + * + * mode 0 = normal, 1 = multiply, 2 = screen, 3 = difference: the four + * the original's Blend popup offers first. + */ +static int +ApplyImageBlend(int amount, int mode) +{ + unsigned char *rgb, *below = NULL; + int w, h, x, y, c, i; + + if (amount < 0) + amount = 0; + if (amount > 100) + amount = 100; + LayerEnsure(); + if (nlayers < 2 || curLayer < 1) { + return -1; /* nothing below the current layer */ + } + LayerAbsorb(); + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if (w != layerW || h != layerH) { + free(rgb); + return -1; + } + for (i = curLayer - 1; i >= 0; i--) + if (layers[i].rgb != NULL && layers[i].visible) { + below = layers[i].rgb; + break; + } + if (below == NULL) { + free(rgb); + return -1; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + const unsigned char *s = + below + ((size_t) y * w + x) * 3; + + for (c = 0; c < 3; c++) { + int v = s[c]; + int o = d[c]; + + switch (mode) { + case 1: v = v * o / 255; break; + case 2: v = 255 - (255 - v) * (255 - o) / 255; + break; + case 3: v = v > o ? v - o : o - v; break; + default: break; + } + /* the amount blends the result back over the + * picture: 100% is the blend alone */ + d[c] = (unsigned char) (o + + (v - o) * amount / 100); + } + } + SetImageRGB(rgb, w, h); + free(rgb); + OpDone("apply image"); + return 0; +} + static void ApplyImageOk(const int *v) { - /* one document: the picture is applied to itself, which is its - * own amount of nothing. Be honest about it. */ - fprintf(stderr, "dtphoto: apply image needs a second layer " - "(%d%% would be a no-op)\n", v[0]); + if (ApplyImageBlend(v[0], 0) != 0) + SetScratch("Apply Image needs a layer below the current " + "one."); +} + +/* + * Calculations mixes two channels of the picture into one: the + * original's Source 1 / Source 2 / Blending popups. Channel n is 0=R, + * 1=G, 2=B. mode follows Apply Image. The result lands in the + * destination channel, which is Source 1's, as in the original. + */ +static int +CalculationsMix(int ch1, int ch2, int mode, int amount) +{ + unsigned char *rgb, *out; + int w, h, x, y; + + if (ch1 < 0 || ch1 > 2 || ch2 < 0 || ch2 > 2) + return -1; + if (amount < 0) + amount = 0; + if (amount > 100) + amount = 100; + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if ((out = malloc((size_t) w * h * 3)) == NULL) { + free(rgb); + return -1; + } + memcpy(out, rgb, (size_t) w * h * 3); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = out + ((size_t) y * w + x) * 3; + const unsigned char *s = rgb + ((size_t) y * w + x) * 3; + int a = s[ch1], b = s[ch2], v; + + switch (mode) { + case 1: v = a * b / 255; break; + case 2: v = 255 - (255 - a) * (255 - b) / 255; break; + case 3: v = a > b ? a - b : b - a; break; + default: v = b; break; + } + d[ch1] = (unsigned char) (a + (v - a) * amount / 100); + } + SetImageRGB(out, w, h); + free(out); + free(rgb); + OpDone("calculations"); + return 0; } static void CalculationsOk(const int *v) { - fprintf(stderr, "dtphoto: calculations needs two channels " - "(%d%% would be a no-op)\n", v[0]); + if (CalculationsMix(0, 1, v[1], v[0]) != 0) + SetScratch("Calculations could not read the picture."); } /* Arbitrary rotation: bilinear, the background fills the corners */ @@ -4946,36 +5625,6 @@ ShowHistogram(void) static int viewGuides = 1; -/* --------------------------------------------------------------- layers */ - -/* - * The layer stack. Photoshop 3 keeps a list of layers per picture and every - * Layer command changes that list, not the pixels. dtphoto had none, so the - * whole Layer menu was decoration. A layer is a name, an RGB buffer and the - * three flags the menu needs: visible, opacity, mode. `image` stays the one - * surface every other command draws on, so painting, saving and the filters - * do not have to know that layers exist. - * - * 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 LayerAbsorb(void); static void LayerInvalidate(void) @@ -5006,7 +5655,7 @@ LayerEnsure(void) layers[0].rgb = rgb; snprintf(layers[0].name, sizeof layers[0].name, "Background"); layers[0].visible = 1; - layers[0].opacity = 100; + layers[0].opacity = layerOpacityDefault; layers[0].mode = 0; nlayers = 1; curLayer = 0; @@ -9144,16 +9793,24 @@ FileOk(Widget w, XtPointer data, XtPointer call) path != NULL) { int save = fileSave; - if (save ? (SaveImage(path) == 0) : (OpenImage(path) == 0)) { + if (save == 3 ? (ExportImage(path) == 0) : + save == 2 ? (PlaceImage(path) == 0) : + save == 1 ? (SaveImage(path) == 0) : (OpenImage(path) == 0)) { fprintf(stderr, "dtphoto: %s %s\n", - save ? "saved" : "opened", path); + save == 3 ? "exported" : + save == 2 ? "placed" : + save == 1 ? "saved" : "opened", path); UpdateStatus(); RenderDoc(canvas); } else { Widget d = XmCreateErrorDialog(top, "error", NULL, 0); XtVaSetValues(d, XmNmessageString, - XmLoc(save ? + XmLoc(save == 3 ? + Tr(8, "The picture could not be exported.") : + save == 2 ? + Tr(7, "Nothing could be placed.") : + save == 1 ? Tr(5, "The picture could not be saved.") : Tr(4, "The picture could not be opened.")), NULL); XtUnmanageChild(XmMessageBoxGetChild(d, @@ -9172,6 +9829,10 @@ FileCancel(Widget w, XtPointer data, XtPointer call) XtDestroyWidget(XtParent(w)); } +/* + * save: 0 opens a picture into this document, 1 writes the document + * out, 2 places a picture on top of it (File > Place). + */ static void FileDialog(int save) { @@ -9232,6 +9893,39 @@ DumpBoundMenu(const Menu *m) } } +/* A label a person can click that resolves to no command is a dead entry. + * This counts them, so the test can refuse a menu the program only + * pretends to carry out. */ +static int +CountUnboundMenu(const Menu *m, int *nitems) +{ + int j, bad = 0; + + for (j = 0; j < m->count; j++) { + const Item *it = &m->items[j]; + + if (it->separator) + continue; + if (it->sub != NULL) { + bad += CountUnboundMenu(it->sub, nitems); + continue; + } + if (nitems != NULL) + (*nitems)++; + if (it->cmd == CMD_NONE) { + fprintf(stderr, "dtphoto: unbound %s\n", it->path); + bad++; + } + } + return bad; +} + +static int +CountUnbound(const Menu *m, int *nitems) +{ + return CountUnboundMenu(m, nitems); +} + /* the list of menu entries the program can actually carry out: the reader * fills Item.cmd, so this is what is bound, not what a document claims */ static void @@ -9485,6 +10179,133 @@ CustomOk(const int *v) OpDone("custom"); } +/* ------------------------------------------------- selection on disk */ + +/* + * A selection is written beside the picture as a P5 (grey PGM) with one + * byte per document pixel: 0 is outside, 255 is fully inside, and the + * values between are the feathered edge. The name is the picture's plus + * ".sel", so a picture carries its selection the way it carries its + * guides in the reference. + */ +static void +SaveSelection(void) +{ + char path[1024]; + FILE *f; + int x, y; + + if (docPath == NULL) { + MessageBox("Save Selection", + "The selection is written beside the picture.\n" + "File > Save As first, then this."); + fprintf(stderr, "dtphoto: save selection, no file\n"); + return; + } + if (!selMaskAny) { + SetScratch("There is no selection to save."); + fprintf(stderr, "dtphoto: save selection, empty\n"); + return; + } + snprintf(path, sizeof path, "%s.sel", docPath); + if ((f = fopen(path, "wb")) == NULL) { + SetScratch("The selection could not be written."); + fprintf(stderr, "dtphoto: save selection, write\n"); + return; + } + fprintf(f, "P5\n%d %d\n255\n", docW, docH); + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + fputc(MaskCover(x, y), f); + fclose(f); + SetScratch("Selection saved: %s", path); + fprintf(stderr, "dtphoto: selection saved %s\n", path); +} + +/* + * Read what SaveSelection wrote. The size must be the document's: a + * selection for another picture would land on the wrong pixels, and a + * silent misfit is worse than a refusal. + */ +static void +LoadSelection(void) +{ + char path[1024]; + FILE *f; + int w = 0, h = 0, maxv = 0, x, y; + + if (docPath == NULL) { + MessageBox("Load Selection", + "Select > Save Selection writes the selection beside the " + "picture, so load needs a saved document first."); + fprintf(stderr, "dtphoto: load selection, no file\n"); + return; + } + snprintf(path, sizeof path, "%s.sel", docPath); + if ((f = fopen(path, "rb")) == NULL) { + SetScratch("No saved selection for this document."); + fprintf(stderr, "dtphoto: load selection, %s missing\n", path); + return; + } + if (fscanf(f, "P5 %d %d %d", &w, &h, &maxv) != 3 || + w != docW || h != docH) { + fclose(f); + SetScratch("The saved selection does not fit this picture."); + fprintf(stderr, "dtphoto: load selection, size\n"); + return; + } + fgetc(f); /* the single whitespace after the header */ + if (MaskAlloc() != 0) { + fclose(f); + return; + } + MaskClear(); + /* + * The coverage layer, before the bits: MaskClear frees it, so a + * load that wrote the bytes first would drop every feathered value + * and keep only the hard edge. + */ + if (CoverAlloc() != 0) { + fclose(f); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int c = fgetc(f); + + if (c == EOF) + break; + selCover[(size_t) y * w + x] = (unsigned char) c; + if (c > 0) + MaskSet(x, y, 1); + } + fclose(f); + /* the marquee follows the mask's bounding box */ + { + int mx0 = w, my0 = h, mx1 = 0, my1 = 0, any = 0; + + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (MaskGet(x, y)) { + any = 1; + if (x < mx0) mx0 = x; + if (y < my0) my0 = y; + if (x > mx1) mx1 = x; + if (y > my1) my1 = y; + } + if (any) { + selX0 = mx0; + selY0 = my0; + selX1 = mx1 + 1; + selY1 = my1 + 1; + hasSel = 1; + } + } + RenderDoc(canvas); + SetScratch("Selection loaded: %s", path); + fprintf(stderr, "dtphoto: selection loaded %s\n", path); +} + static void MenuCallback(Widget w, XtPointer data, XtPointer call) { @@ -9926,14 +10747,12 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) case CMD_RULERS: showRulers = !showRulers; RenderDoc(canvas); - RenderDoc(canvas); fprintf(stderr, "dtphoto: rulers %s\n", showRulers ? "on" : "off"); break; case CMD_GRID: showGrid = !showGrid; RenderDoc(canvas); - RenderDoc(canvas); fprintf(stderr, "dtphoto: grid %s\n", showGrid ? "on" : "off"); break; @@ -9969,14 +10788,7 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) /* ---------------- the rest of the reference's menu list ---------- */ /* File */ case CMD_OPENAS: FileDialog(0); break; - case CMD_PLACE: - MessageBox("Place", - "Place reads a second picture into this document.\n" - "This build keeps one picture per window, so there is " - "nothing to place into: open the second picture and " - "use Edit > Copy, then Edit > Paste."); - fprintf(stderr, "dtphoto: place (one picture per window)\n"); - break; + case CMD_PLACE: FileDialog(2); break; case CMD_SAVECOPY: FileDialog(1); break; case CMD_ACQUIRE: MessageBox("Acquire", @@ -9985,11 +10797,12 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) fprintf(stderr, "dtphoto: acquire (no source)\n"); break; case CMD_EXPORT: - MessageBox("Export", - "Export writes a file for another program.\n" - "This build writes BMP, PNG, JPEG and GIF through " - "File > Save As."); - fprintf(stderr, "dtphoto: export\n"); + /* + * Export is Save As without adopting the name: the format + * comes from the ending (PNG, BMP, JPEG, GIF, XPM), and the + * document keeps writing to its own file afterwards. + */ + FileDialog(3); break; /* Edit */ case CMD_PASTEINTO: @@ -10002,12 +10815,16 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) OpDone("paste layer"); break; case CMD_CREATEPUB: + /* + * The original's Publisher hands the picture to a document + * program. What this build can honour is the exchange file + * itself: an EPS, which every layout program reads. So the + * command writes one instead of refusing. + */ + PublishEPS(); + break; case CMD_PUBOPTS: - MessageBox("Publisher", - "Publisher writes an Acrobat exchange file.\n" - "There is no PDF writer in this build; File > Save As " - "writes BMP, PNG, JPEG and GIF."); - fprintf(stderr, "dtphoto: publisher (no pdf writer)\n"); + PublisherOptions(); break; case CMD_SNAPSHOT: XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); @@ -10039,9 +10856,6 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) OpGamutWarning(1); OpDone("gamut warning"); break; - case CMD_IMAGEDUP: - DuplicateDoc(); - break; /* Image > Effects */ case CMD_EFFECTSCALE: { char buf[32]; @@ -10095,10 +10909,7 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) curLayer + 1, nlayers); break; case CMD_ADJOPTS: - MessageBox("Adjustment Options", - "An adjustment layer would carry a correction.\n" - "This build corrects the layer itself: Image > Adjust."); - fprintf(stderr, "dtphoto: adjustment options\n"); + AdjustmentOptions(); break; case CMD_DROPSHADOW: OpLayerEffect(0, 4, 60, 4, 4); @@ -10122,8 +10933,9 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) break; case CMD_TYPEH: case CMD_TYPEV: - /* the type tool of the toolbox, which is how text gets in */ - currentTool = 7; + /* the type tool of the toolbox, which is how text gets in: + * slot 8 in toolNames, not 7 -- that one is crop */ + currentTool = 8; fprintf(stderr, "dtphoto: tool %s\n", toolNames[currentTool]); break; case CMD_LAYERSCALE: @@ -10200,113 +11012,12 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) RenderDoc(canvas); fprintf(stderr, "dtphoto: edges hidden\n"); break; - case CMD_LOADSEL: { - /* the .sel file Save Selection wrote, beside the picture */ - char path[1024]; - FILE *f; - int w = 0, h = 0, maxv = 0, x, y; - - if (docPath == NULL) { - MessageBox("Load Selection", - "Select > Save Selection writes the selection " - "beside the picture, so load needs a saved " - "document first."); - fprintf(stderr, "dtphoto: load selection, no file\n"); - break; - } - snprintf(path, sizeof path, "%s.sel", docPath); - if ((f = fopen(path, "rb")) == NULL) { - SetScratch("No saved selection for this document."); - fprintf(stderr, "dtphoto: load selection, %s missing\n", - path); - break; - } - if (fscanf(f, "P5 %d %d %d", &w, &h, &maxv) != 3 || - w != docW || h != docH) { - fclose(f); - SetScratch("The saved selection does not fit this " - "picture."); - fprintf(stderr, "dtphoto: load selection, size\n"); - break; - } - fgetc(f); /* the single whitespace after the header */ - if (MaskAlloc() != 0) { - fclose(f); - break; - } - MaskClear(); - for (y = 0; y < h; y++) - for (x = 0; x < w; x++) { - int c = fgetc(f); - - if (c == EOF) - break; - if (c > 0) { - MaskSet(x, y, 1); - if (selCover != NULL) - selCover[(size_t) y * w + x] = - (unsigned char) c; - } - } - fclose(f); - /* the marquee follows the mask's bounding box */ - { - int mx0 = w, my0 = h, mx1 = 0, my1 = 0, any = 0; - - for (y = 0; y < h; y++) - for (x = 0; x < w; x++) - if (MaskGet(x, y)) { - any = 1; - if (x < mx0) mx0 = x; - if (y < my0) my0 = y; - if (x > mx1) mx1 = x; - if (y > my1) my1 = y; - } - if (any) { - selX0 = mx0; - selY0 = my0; - selX1 = mx1 + 1; - selY1 = my1 + 1; - hasSel = 1; - } - } - RenderDoc(canvas); - SetScratch("Selection loaded: %s", path); - fprintf(stderr, "dtphoto: selection loaded %s\n", path); + case CMD_LOADSEL: + LoadSelection(); break; - } - case CMD_SAVESEL: { - char path[1024]; - FILE *f; - int x, y; - - if (docPath == NULL) { - MessageBox("Save Selection", - "The selection is written beside the picture.\n" - "File > Save As first, then this."); - fprintf(stderr, "dtphoto: save selection, no file\n"); - break; - } - if (!selMaskAny) { - SetScratch("There is no selection to save."); - fprintf(stderr, "dtphoto: save selection, empty\n"); - break; - } - snprintf(path, sizeof path, "%s.sel", docPath); - if ((f = fopen(path, "wb")) == NULL) { - SetScratch("The selection could not be written."); - fprintf(stderr, "dtphoto: save selection, write\n"); - break; - } - fprintf(f, "P5\n%d %d\n255\n", docW, docH); - for (y = 0; y < docH; y++) - for (x = 0; x < docW; x++) - fputc(MaskCover(x, y), f); - fclose(f); - SetScratch("Selection saved: %s", path); - fprintf(stderr, "dtphoto: selection saved %s\n", path); + case CMD_SAVESEL: + SaveSelection(); break; - } /* Filter */ case CMD_LASTFILTER: RepeatLastFilter(); @@ -10351,7 +11062,6 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) case CMD_SHOWGUIDES: showGuides = !showGuides; RenderDoc(canvas); - RenderDoc(canvas); fprintf(stderr, "dtphoto: guides %s\n", showGuides ? "on" : "off"); break; @@ -10363,8 +11073,6 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) case CMD_LOCKGUIDES: lockGuides = !lockGuides; /* a locked guide cannot be moved by a drag */ - if (lockGuides) - fprintf(stderr, "dtphoto: guides locked\n"); fprintf(stderr, "dtphoto: guides %s\n", lockGuides ? "locked" : "unlocked"); break; @@ -10372,7 +11080,6 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) GuidesClear(); showGuides = 0; RenderDoc(canvas); - RenderDoc(canvas); fprintf(stderr, "dtphoto: guides cleared\n"); break; case CMD_SNAPGRID: @@ -10391,24 +11098,13 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) ShowLayers(); break; case CMD_SHOWCHANNELS: - MessageBox("Channels", - "An RGB picture has three channels.\n" - "This build keeps them together in one buffer, so the " - "palette would show three copies of the same picture."); - fprintf(stderr, "dtphoto: channels\n"); + ShowChannels(); break; case CMD_SHOWPATHS: - MessageBox("Paths", - "There is no pen tool in this build, so there are no " - "paths to list."); - fprintf(stderr, "dtphoto: paths\n"); + ShowPaths(); break; case CMD_SHOWCOMMANDS: - MessageBox("Commands", - "The commands this build carries out are listed in " - "dtphoto -menutest, one path per line.\n" - "Filter > Last Filter repeats the last one."); - fprintf(stderr, "dtphoto: commands\n"); + ShowCommands(); break; default: fprintf(stderr, "dtphoto: %s\n", label); @@ -10442,6 +11138,7 @@ main(int argc, char **argv) top = XtVaAppInitialize(&app, "Dtphoto", opts, XtNumber(opts), &argc, argv, fallbacks, NULL); + appctx = app; /* only valid after the call that fills it */ dpy = XtDisplay(top); InitColor(); if (XGetDefault(dpy, "dtphoto", "version") != NULL) @@ -10972,12 +11669,454 @@ main(int argc, char **argv) return bad == 0 ? 0 : 1; } + + /* + * Save Selection / Load Selection round trip. A half-covered band + * goes out and comes back: every byte must be the byte written, + * which is what the feathered edge depends on. The document is + * given a path so the file lands beside it, and the mask is + * cleared in between so a load that reads nothing cannot pass. + */ + if (argc >= 3 && strcmp(argv[1], "-seltest") == 0) { + int w = 12, h = 8, x, y, bad = 0; + unsigned char *rgb; + int n; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto seltest: FAIL malloc\n"); + return 1; + } + for (n = 0; n < w * h; n++) + rgb[n * 3] = rgb[n * 3 + 1] = rgb[n * 3 + 2] = 200; + SetImageRGB(rgb, w, h); + free(rgb); + if (MaskAlloc() != 0) { + printf("dtphoto seltest: FAIL alloc\n"); + return 1; + } + MaskClear(); + selMaskAny = 1; + for (y = 2; y < 6; y++) + for (x = 3; x < 9; x++) + MaskSet(x, y, 1); + hasSel = 1; + selX0 = 3; selY0 = 2; selX1 = 9; selY1 = 6; + /* the cover layer first, then the soft edge written into it */ + if (CoverAlloc() != 0) { + printf("dtphoto seltest: FAIL cover alloc\n"); + return 1; + } + for (y = 2; y < 6; y++) + for (x = 3; x < 9; x++) + selCover[(size_t) y * w + x] = + (unsigned char) (x == 3 ? 64 : + x == 4 ? 160 : 255); + + docPath = argv[2]; + SaveSelection(); + /* what was saved must be there and be a P5 of this size */ + { + char sp[1024]; + FILE *f; + int sw = 0, sh = 0, sm = 0; + + snprintf(sp, sizeof sp, "%s.sel", docPath); + if ((f = fopen(sp, "rb")) == NULL) { + printf("dtphoto seltest: %s not written FAIL\n", + sp); + bad++; + } else { + if (fscanf(f, "P5 %d %d %d", &sw, &sh, &sm) != 3 + || sw != w || sh != h || sm != 255) { + printf("dtphoto seltest: header %dx%d " + "max %d FAIL\n", sw, sh, sm); + bad++; + } else + printf("dtphoto seltest: header %dx%d " + "max 255 ok\n", sw, sh); + fclose(f); + } + } + /* wipe the mask: a load that reads nothing stays empty */ + MaskClear(); + selMaskAny = 0; + hasSel = 0; + LoadSelection(); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int want = (x >= 3 && x < 9 && y >= 2 && y < 6); + int got = MaskGet(x, y); + + if (got != want) { + printf("dtphoto seltest: mask %d,%d " + "got %d want %d FAIL\n", x, y, got, + want); + bad++; + } + } + /* the feather's bytes must survive, not just the bits */ + if (MaskGet(3, 2) && MaskCover(3, 2) != 64) { + printf("dtphoto seltest: cover 3,2 = %d want 64 FAIL\n", + MaskCover(3, 2)); + bad++; + } + if (MaskCover(5, 4) != 255) { + printf("dtphoto seltest: cover 5,4 = %d want 255 FAIL\n", + MaskCover(5, 4)); + bad++; + } + if (!hasSel || selX0 != 3 || selY0 != 2 || + selX1 != 9 || selY1 != 6) { + printf("dtphoto seltest: marquee %d,%d..%d,%d want " + "3,2..9,6 FAIL\n", selX0, selY0, selX1, selY1); + bad++; + } + docPath = NULL; + printf("dtphoto seltest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + + /* + * The Channels and Paths palettes and the Publisher's resolution: the + * three places where a palette could so easily show a pretty picture + * of nothing. Each is checked against numbers worked out here, and + * the EPS is read back to see whether the resolution reached the + * header -- a field that only changes a variable is not a feature. + */ + if (argc >= 2 && strcmp(argv[1], "-paneltest") == 0) { + unsigned char *rgb, *got; + long lo, hi, distinct, mean; + int w = 4, h = 3, gw, gh, x, y, bad = 0; + char eps[256], line[256]; + FILE *fp; + double want; + + /* R: 0,10,20,30 across; G: 0,20,40 down; B stays 60 */ + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto paneltest: 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] = (unsigned char) (x * 10); + d[1] = (unsigned char) (y * 20); + d[2] = 60; + } + SetImageRGB(rgb, w, h); + + /* red: values 0,10,20,30 -> lo 0, hi 30, mean 15, 4 levels */ + ChannelMeasure(0, &lo, &hi, &distinct, &mean); + if (lo != 0 || hi != 30 || mean != 15 || distinct != 4) { + printf("dtphoto paneltest: red %ld..%ld mean %ld " + "%ld levels, want 0..30 mean 15 4 FAIL\n", lo, hi, + mean, distinct); + bad++; + } else + printf("dtphoto paneltest: red %ld..%ld mean %ld " + "%ld levels ok\n", lo, hi, mean, distinct); + /* blue: constant 60 -> one level, mean 60 */ + ChannelMeasure(2, &lo, &hi, &distinct, &mean); + if (lo != 60 || hi != 60 || mean != 60 || distinct != 1) { + printf("dtphoto paneltest: blue %ld..%ld mean %ld " + "%ld levels, want 60 constant FAIL\n", lo, hi, mean, + distinct); + bad++; + } else + printf("dtphoto paneltest: blue %ld..%ld mean %ld " + "%ld level ok\n", lo, hi, mean, distinct); + + /* the path list reads the lasso's run, not a hard-coded name */ + lassoN = 0; + LassoAdd(2, 3); + LassoAdd(9, 3); + LassoAdd(9, 8); + LassoAdd(2, 8); + if (lassoN != 4) { + printf("dtphoto paneltest: lasso %d points FAIL\n", + lassoN); + bad++; + } else + printf("dtphoto paneltest: path 4 points ok\n"); + lassoN = 0; + + /* + * The Publisher's resolution: 4 x 3 at 72 dpi is 4 x 3 points, + * at 288 dpi a quarter of that. The EPS header has to carry + * the second number. + */ + snprintf(eps, sizeof eps, "/tmp/dtphoto-panel.eps"); + free(docPath); + docPath = strdup("/tmp/dtphoto-panel"); + epsResolution = 72; + if (PublishEPS() != 0) { + printf("dtphoto paneltest: publish 72 FAIL\n"); + bad++; + } else + printf("dtphoto paneltest: publish 72 ok\n"); + epsResolution = 288; + if (PublishEPS() != 0) { + printf("dtphoto paneltest: publish 288 FAIL\n"); + bad++; + } else + printf("dtphoto paneltest: publish 288 ok\n"); + snprintf(eps, sizeof eps, "/tmp/dtphoto-panel.eps"); + fp = fopen(eps, "r"); + if (fp == NULL) { + printf("dtphoto paneltest: %s missing FAIL\n", eps); + bad++; + } else { + want = 4.0 * 72.0 / 288.0; /* 1 point wide */ + while (fgets(line, sizeof line, fp) != NULL) + if (strncmp(line, "%%BoundingBox:", 14) == 0) { + /* llx lly urx ury -- the width is the + * third number, not the first */ + double a[4]; + double bw, bh; + int n = sscanf(strchr(line, ':') + 1, + "%lf %lf %lf %lf", &a[0], &a[1], + &a[2], &a[3]); + + if (n != 4) { + printf("dtphoto paneltest: " + "bbox unreadable FAIL\n"); + bad++; + continue; + } + bw = a[2] - a[0]; + bh = a[3] - a[1]; + fprintf(stderr, "dtphoto paneltest: " + "bbox %.0fx%.0f points, want " + "%.0fx%0.f\n", bw, bh, want, + want * h / w); + if ((int) (want + 0.5) != + (int) (bw + 0.5)) { + printf("dtphoto paneltest: " + "bbox width %.2f want %.2f " + "FAIL\n", bw, want); + bad++; + } else + printf("dtphoto paneltest: " + "bbox %.0f points ok\n", bw); + break; + } + fclose(fp); + if (getenv("DTPHOTO_KEEP_EPS") == NULL) + unlink(eps); + } + + /* and the picture must not have changed by any of it */ + if (GetImageRGB(&got, &gw, &gh) == 0) { + if (gw != w || gh != h || + memcmp(got, rgb, (size_t) w * h * 3) != 0) { + printf("dtphoto paneltest: picture changed " + "FAIL\n"); + bad++; + } else + printf("dtphoto paneltest: picture unchanged " + "ok\n"); + free(got); + } + free(rgb); + printf("dtphoto paneltest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + + /* + * Printing: the document goes to the queue as a BMP. With + * MCDE_PRINT_COMMAND naming a tool that keeps what it is given, the + * whole path -- save, spool, reap -- is checked without a printer, + * which is the point: a print path that only claims to work is what + * this replaces. The last argument lp(1) is given is the file. + */ + if (argc >= 2 && strcmp(argv[1], "-placetest") == 0) { + unsigned char *rgb, *got; + int w, h, gw, gh, bad = 0; + const char *small = "/tmp/place-small.png"; + + /* a 32x24 document in mid grey */ + w = 32; h = 24; + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) + return 1; + memset(rgb, 100, (size_t) w * h * 3); + SetImageRGB(rgb, w, h); + free(rgb); + + /* + * The picture to place: 8x8 pure white, written through the + * same writer the program uses, so the test covers the file + * path and not only the arithmetic. + */ + { + unsigned char *s8 = malloc(8 * 8 * 3); + int i; + + if (s8 == NULL) + return 1; + for (i = 0; i < 8 * 8; i++) + s8[i * 3] = s8[i * 3 + 1] = + s8[i * 3 + 2] = 255; + if (McdeThumbWrite(small, s8, 8, 8) != 0) { + printf("dtphoto placetest: FAIL write\n"); + free(s8); + return 1; + } + free(s8); + } + if (PlaceImage(small) != 0) { + printf("dtphoto placetest: FAIL place\n"); + return 1; + } + if (GetImageRGB(&got, &gw, &gh) != 0) { + printf("dtphoto placetest: FAIL read\n"); + return 1; + } + /* the document keeps its size: a place adds, it does not open */ + if (gw != w || gh != h) { + printf("dtphoto placetest: size %dx%d want %dx%d FAIL\n", + gw, gh, w, h); + bad++; + } + /* + * 8x8 centred in 32x24 sits at x = 12, y = 8. The middle of + * the placed square is white; a corner of the document is + * still the grey it was. + */ + { + unsigned char *c = got + ((size_t) 12 * gw + 16) * 3; + unsigned char *o = got + ((size_t) 1 * gw + 1) * 3; + + if (c[0] < 250 || c[1] < 250 || c[2] < 250) { + printf("dtphoto placetest: centre %d,%d,%d " + "want white FAIL\n", c[0], c[1], c[2]); + bad++; + } + if (abs((int) o[0] - 100) > 2) { + printf("dtphoto placetest: outside %d,%d,%d " + "want grey FAIL\n", o[0], o[1], o[2]); + bad++; + } + } + /* + * A picture larger than the document is fitted, not clipped: + * 64x48 into 32x24 lands as 32x24, so every pixel of the + * document is covered and the grey is gone. + */ + { + unsigned char *big = malloc(64 * 48 * 3); + int i, grey = 0; + + if (big == NULL) { + printf("dtphoto placetest: FAIL big\n"); + return 1; + } + for (i = 0; i < 64 * 48; i++) + big[i * 3] = big[i * 3 + 1] = + big[i * 3 + 2] = 255; + if (McdeThumbWrite("/tmp/place-big.png", big, 64, 48) + != 0) { + printf("dtphoto placetest: FAIL big write\n"); + free(big); + return 1; + } + free(big); + if (PlaceImage("/tmp/place-big.png") != 0) { + printf("dtphoto placetest: FAIL big place\n"); + return 1; + } + if (GetImageRGB(&got, &gw, &gh) != 0) { + printf("dtphoto placetest: FAIL big read\n"); + return 1; + } + if (gw != 32 || gh != 24) { + printf("dtphoto placetest: big size %dx%d " + "FAIL\n", gw, gh); + bad++; + } + for (i = 0; i < gw * gh; i++) + if (abs((int) got[i * 3] - 100) < 20) + grey++; + if (grey != 0) { + printf("dtphoto placetest: big left %d grey " + "pixels, want the picture to cover FAIL\n", + grey); + bad++; + } + free(got); + } + printf("dtphoto placetest: %s\n", bad ? "FAIL" : "PASS"); + return bad ? 1 : 0; + } + if (argc >= 3 && strcmp(argv[1], "-printtest") == 0) { + const char *fake = getenv("MCDE_PRINT_COMMAND"); + const char *out = argv[2]; + McdeSysJob *job; + int fd, rc, waited = 0; + char spool[64]; + struct stat st; + + if (fake == NULL) { + printf("dtphoto printtest: MCDE_PRINT_COMMAND unset, " + "would need a printer FAIL\n"); + return 1; + } + snprintf(spool, sizeof spool, "/tmp/dtphoto-print-%d.bmp", + (int) getpid()); + if (SaveBMP(spool) != 0) { + printf("dtphoto printtest: save %s FAIL\n", spool); + return 1; + } + job = mcde_sys_print_file(spool, NULL); + if (job == NULL) { + printf("dtphoto printtest: spool refused: %s FAIL\n", + mcde_sys_error()); + unlink(spool); + return 1; + } + fd = mcde_sys_job_fd(job); + { /* wait for the tool, as the event loop would */ + fd_set rs; + struct timeval tv; + + while (waited < 100) { + FD_ZERO(&rs); + FD_SET(fd, &rs); + tv.tv_sec = 0; + tv.tv_usec = 100000; + if (select(fd + 1, &rs, NULL, NULL, &tv) > 0) + break; + waited++; + } + } + rc = mcde_sys_job_finish(job); + unlink(spool); + if (rc != MCDE_SYS_OK) { + printf("dtphoto printtest: lp failed: %s FAIL\n", + mcde_sys_error()); + return 1; + } + if (stat(out, &st) != 0 || st.st_size < 54) { + printf("dtphoto printtest: %s never reached the " + "printer FAIL\n", out); + return 1; + } + printf("dtphoto printtest: %ld bytes spooled to the printer " + "ok\n", (long) st.st_size); + printf("dtphoto printtest: PASS\n"); + return 0; + } + /* The menu bindings, without a mouse: every path the program claims * to carry out must resolve to the command the callback switches * on. A path that resolves to CMD_NONE is an entry that does * nothing but print its own label. */ if (argc >= 2 && strcmp(argv[1], "-menutest") == 0) { int i, bad = 0, ntab = 0, ndup = 0; + int nmenu = 0, nbind = 0; /* * Every entry of the command table must resolve to its own @@ -11007,6 +12146,23 @@ main(int argc, char **argv) printf("dtphoto menutest: unknown path bound FAIL\n"); bad++; } + /* + * The table is only half the story: the menu the program + * actually reads must be covered by it. A line in the + * menu file with no command is a label a person can click + * that does nothing, which is what this test is for. + */ + { + int i; + nmenus = 0; + ReadMenus("dtphoto.menu"); + for (i = 0; i < nmenus; i++) + nbind += CountUnbound(&menus[i], &nmenu); + } + printf("dtphoto menutest: %d menu items, %d unbound\n", + nmenu, nbind); + if (nbind != 0) + bad++; printf("dtphoto menutest: %d paths, %d doubled, %d bad\n", ntab, ndup, bad); printf("dtphoto menutest: %s\n", @@ -12100,6 +13256,141 @@ main(int argc, char **argv) return bad == 0 ? 0 : 1; } + if (argc >= 2 && strcmp(argv[1], "-exporttest") == 0) { + unsigned char *rgb, *got; + int w, h, gw, gh, x, y, c, bad = 0; + char before[256]; + static const char *const exts[] = { + "png", "bmp", "jpg", "gif", "xpm", NULL + }; + int e; + + w = 12; h = 9; + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) + return 1; + 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 * 20); + p[1] = (unsigned char) (y * 25); + p[2] = (unsigned char) (255 - x * 15); + } + SetImageRGB(rgb, w, h); + + /* + * Export must not adopt the name: the document keeps writing + * to its own file after an export. That is the difference + * from Save As, and the reason the two are separate entries. + */ + snprintf(before, sizeof before, "%s", + docPath != NULL ? docPath : ""); + + for (e = 0; exts[e] != NULL; e++) { + char path[128]; + int px; + + snprintf(path, sizeof path, "/tmp/exp.%s", exts[e]); + remove(path); + if (ExportImage(path) != 0) { + printf("dtphoto exporttest: %s FAIL write\n", + exts[e]); + bad++; + continue; + } + /* + * Every export but JPEG comes back exactly: this + * picture has 108 colours, so GIF keeps its own table + * instead of quantising. + */ + if (strcmp(exts[e], "jpg") == 0) { + FILE *f = fopen(path, "rb"); + + if (f == NULL || fgetc(f) != 0xFF) { + printf("dtphoto exporttest: jpg FAIL " + "magic\n"); + bad++; + } + if (f != NULL) + fclose(f); + continue; + } + /* + * Reading the export back is a test step, not an + * export: it legitimately adopts the name, so the + * document name is checked right here, before the + * first read. + */ + if (e == 0 && + strcmp(before, docPath != NULL ? docPath : "") != 0) { + printf("dtphoto exporttest: export renamed the " + "document FAIL\n"); + bad++; + } + if (OpenImage(path) != 0) { + printf("dtphoto exporttest: %s FAIL read\n", + exts[e]); + bad++; + continue; + } + if (GetImageRGB(&got, &gw, &gh) != 0) { + printf("dtphoto exporttest: %s FAIL get\n", + exts[e]); + bad++; + continue; + } + if (gw != w || gh != h) { + printf("dtphoto exporttest: %s size %dx%d " + "want %dx%d FAIL\n", exts[e], gw, gh, + w, h); + bad++; + } + px = 0; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + for (c = 0; c < 3; c++) + if (got[((size_t) y * w + x) * 3 + + c] != + rgb[((size_t) y * w + x) * 3 + + c]) + px++; + if (px != 0) { + printf("dtphoto exporttest: %s %d bytes " + "differ FAIL\n", exts[e], px); + bad++; + } + free(got); + } + + + + /* Publisher writes an EPS; it must start with the right line */ + if (docPath != NULL) { + char ep[320]; + FILE *f; + char line[64]; + + snprintf(ep, sizeof ep, "%s.eps", docPath); + remove(ep); + if (PublishEPS() != 0) { + printf("dtphoto exporttest: eps FAIL write\n"); + bad++; + } else if ((f = fopen(ep, "rb")) == NULL || + fgets(line, sizeof line, f) == NULL || + strncmp(line, "%!PS-Adobe-3.0 EPSF-3.0", 20) != 0) { + printf("dtphoto exporttest: eps FAIL header\n"); + bad++; + if (f != NULL) + fclose(f); + } else + fclose(f); + } + free(rgb); + printf("dtphoto exporttest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + if (argc >= 2 && strcmp(argv[1], "-optest") == 0) { unsigned char *rgb; int w = 6, h = 4, x, y, bad = 0; diff --git a/ext/src/mcde/programs/dtphoto/dtphoto.msg b/ext/src/mcde/programs/dtphoto/dtphoto.msg index ed8ed08283..49e01cb3ba 100644 --- a/ext/src/mcde/programs/dtphoto/dtphoto.msg +++ b/ext/src/mcde/programs/dtphoto/dtphoto.msg @@ -8,3 +8,5 @@ $set 1 4 The picture could not be opened. 5 The picture could not be saved. 6 Tool Options +7 Nothing could be placed. +8 The picture could not be exported.