diff --git a/AGENTS.md b/AGENTS.md index 7cd1a792..a3d4ddee 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -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 =` 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`, diff --git a/include/Mcde/Thumb.h b/include/Mcde/Thumb.h index 8d68ba92..9ff13e20 100644 --- a/include/Mcde/Thumb.h +++ b/include/Mcde/Thumb.h @@ -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). */ diff --git a/lib/DtSvc/DtUtil2/McdeThumb.c b/lib/DtSvc/DtUtil2/McdeThumb.c index 88b99ed3..21264081 100644 --- a/lib/DtSvc/DtUtil2/McdeThumb.c +++ b/lib/DtSvc/DtUtil2/McdeThumb.c @@ -29,6 +29,7 @@ #include #include +#include #include #include #include @@ -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; +} diff --git a/lib/DtSvc/DtUtil2/test_thumb.c b/lib/DtSvc/DtUtil2/test_thumb.c index 71c28f89..75783572 100644 --- a/lib/DtSvc/DtUtil2/test_thumb.c +++ b/lib/DtSvc/DtUtil2/test_thumb.c @@ -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); diff --git a/programs/dtphoto/dtphoto.c b/programs/dtphoto/dtphoto.c index 25857c6e..bcc23578 100644 --- a/programs/dtphoto/dtphoto.c +++ b/programs/dtphoto/dtphoto.c @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -36,8 +37,11 @@ #include #include #include +#include #include +#include + #define MAXMENUS 16 #define MAXITEMS 80 #define MAXDEPTH 4 @@ -63,6 +67,9 @@ enum { CMD_NONE, CMD_NEW, CMD_OPEN, CMD_SAVE, CMD_SAVEAS, CMD_QUIT, CMD_UNDO, CMD_ZOOMIN, CMD_ZOOMOUT, CMD_FIT, CMD_ACTUAL, CMD_ALL, CMD_DESELECT, CMD_CLEAR, CMD_FILL, CMD_CROP, CMD_INVERSE, + /* Select > Modify: morphology on the mask */ + CMD_FEATHER, CMD_SELBORDER, CMD_SELSMOOTH, CMD_SELEXPAND, + CMD_SELCONTRACT, CMD_SELGROW, CMD_SELSIMILAR, /* Image: the whole picture, through the RGB buffer */ CMD_INVERT, CMD_DESATURATE, CMD_FLIPH, CMD_FLIPV, CMD_ROT180, CMD_ROT90CW, CMD_ROT90CCW, @@ -70,7 +77,9 @@ enum { /* Filter: convolution, noise, pixelate, tone */ CMD_BLUR, CMD_BLURMORE, CMD_SHARPEN, CMD_SHARPENMORE, CMD_FINDEDGES, CMD_EMBOSS, CMD_SOLARIZE, - CMD_ADDNOISE, CMD_MEDIAN, CMD_MOSAIC + CMD_ADDNOISE, CMD_MEDIAN, CMD_MOSAIC, + /* Window: the Options box */ + CMD_SHOWOPTIONS }; static Menu menus[MAXMENUS]; @@ -97,8 +106,30 @@ static int selX0, selY0, selX1, selY1, hasSel, dragging; static unsigned char *selMask; /* docW * docH bits, 1 = selected */ static size_t selMaskSize; static int selMaskAny; /* some bit is set */ +/* + * The coverage, one byte per pixel, 0 = outside, 255 = fully inside. + * A hard-edged selection has 0 or 255; Feather fills the band between. + * Kept beside the bit mask and not instead of it: the bit mask answers + * "is this pixel in the selection at all", which is what the marquee, the + * wand and Move ask, and a coverage of 3 would answer that with "barely". + * NULL means "no soft edge", and then the bit mask is the whole truth. + */ +static unsigned char *selCover; +static size_t selCoverSize; static unsigned char markRGB[3]; static int markValid; +/* + * The values the Options box shows. They are variables and not constants + * because a brush that is always 3 px wide is a demo, not an editor; the + * numbers here are the ones the code used before the box existed, so a run + * without the box behaves exactly as it did. + */ +static int pencilRadius = 3; /* the pencil and the brush */ +static int eraserRadius = 6; +static int wandTolerance = 32; /* the wand, and Grow/Similar */ +static int retouchRadius = 6; +static int retouchStrength = 60; +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 * screen for the hand (in window coordinates, not document ones) */ @@ -132,6 +163,7 @@ static const char *const toolNames[] = { static void NewPixmaps(void); static void DrawMarquee(Window win, int ox, int oy); static void MaskClear(void); +static int MaskCover(int x, int y); static void RenderDoc(Widget w); static void TypeIntoImage(int x, int y); static void SetZoom(int z); @@ -982,6 +1014,50 @@ OpenBMP(const char *path) return -1; } +/* + * A picture into the document, whatever this program can read. BMP and ICO + * go through the reader above, which knows their palettes; PNG, JPEG, GIF and + * XPM come from libDtSvc (McdeThumbRead), the decoder dtfile uses for its + * miniatures, so this program does not carry a second one + * (ext/src/mcde/AGENTS.md, "The philosophy"). + */ +static int +OpenImage(const char *path) +{ + McdeThumbImage image; + int i; + + if (OpenBMP(path) == 0) + return 0; + memset(&image, 0, sizeof image); + if (McdeThumbRead(path, 0, 0, &image) != 0) + return -1; + i = SetImageRGB(image.rgb, image.width, image.height); + McdeThumbFree(&image); + return i; +} + +/* + * The document into a file. The ending decides: .png and .bmp are written + * by libDtSvc, anything else keeps the BMP writer this program has always + * had, so an old name without an ending still works. + */ +static int +SaveImage(const char *path) +{ + unsigned char *rgb; + int w, h, i; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if (McdeThumbWrite(path, rgb, w, h) == 0) { + free(rgb); + return 0; + } + free(rgb); + return SaveBMP(path); +} + /* ------------------------------------------------------------------ menus */ static void @@ -1107,9 +1183,18 @@ CommandOf(const char *path) if (strcmp(path, "View/Zoom Out") == 0) return CMD_ZOOMOUT; if (strcmp(path, "View/Fit on Screen") == 0) return CMD_FIT; if (strcmp(path, "View/Actual Pixels") == 0) return CMD_ACTUAL; + if (strcmp(path, "Window/Show Options") == 0) return CMD_SHOWOPTIONS; if (strcmp(path, "Select/All") == 0) return CMD_ALL; if (strcmp(path, "Select/None") == 0) return CMD_DESELECT; if (strcmp(path, "Select/Inverse") == 0) return CMD_INVERSE; + /* Select > Modify: shapes the mask, not the picture */ + if (strcmp(path, "Select/Feather...") == 0) return CMD_FEATHER; + if (strcmp(path, "Select/Modify/Border...") == 0) return CMD_SELBORDER; + if (strcmp(path, "Select/Modify/Smooth...") == 0) return CMD_SELSMOOTH; + if (strcmp(path, "Select/Modify/Expand...") == 0) return CMD_SELEXPAND; + if (strcmp(path, "Select/Modify/Contract...") == 0) return CMD_SELCONTRACT; + if (strcmp(path, "Select/Grow") == 0) return CMD_SELGROW; + if (strcmp(path, "Select/Similar") == 0) return CMD_SELSIMILAR; if (strcmp(path, "Edit/Clear") == 0) return CMD_CLEAR; if (strcmp(path, "Edit/Fill...") == 0) return CMD_FILL; if (strcmp(path, "Edit/Crop") == 0) return CMD_CROP; @@ -1340,6 +1425,7 @@ Dab(int x, int y, int r) XFillArc(dpy, image, gc, x - r, y - r, r * 2 + 1, r * 2 + 1, 0, 360 * 64); } + static void LineImage(int x0, int y0, int x1, int y1) { @@ -1660,6 +1746,86 @@ Retouch(int cx, int cy, int radius, int strength, int op) p.rgb = NULL; } +/* + * A tip of paint inside the selection. Painting in the original is always + * clipped to the selection, and a feathered edge mixes instead of cutting, + * so every painting tool goes through here: read the box once, write the + * tip into it, put the box back. `soft` rounds the tip (the brush), a + * square tip is the pencil. + */ +static void +PaintTip(int cx, int cy, int r, Pixel px, int soft) +{ + Patch p; + unsigned char fg[3]; + int x, y; + + if (ReadPatch(&p, cx - r, cy - r, r * 2 + 1, r * 2 + 1) != 0) + return; + PixelToRGB(px, &fg[0], &fg[1], &fg[2]); + for (y = 0; y < p.h; y++) + for (x = 0; x < p.w; x++) { + unsigned char *d = PatchAt(&p, x, y); + int docx = p.x + x, docy = p.y + y; + int cov = 255; + int dx = x - r, dy = y - r; + + if (soft && dx * dx + dy * dy > r * r) + continue; + if (selMaskAny) + cov = MaskCover(docx, docy); + if (cov <= 0) + continue; + d[0] = (unsigned char) (d[0] + + ((int) fg[0] - d[0]) * cov / 255); + d[1] = (unsigned char) (d[1] + + ((int) fg[1] - d[1]) * cov / 255); + d[2] = (unsigned char) (d[2] + + ((int) fg[2] - d[2]) * cov / 255); + } + WritePatch(&p); + free(p.rgb); +} + +/* the pencil: a hard square tip, the brush: a round soft one */ +static void +PencilDot(int x, int y, int r) +{ + PaintTip(x, y, r, fgPixel, 0); +} + +static void +BrushDot(int x, int y, int r) +{ + PaintTip(x, y, r, fgPixel, 1); +} + +static void +EraserDot(int x, int y, int r) +{ + PaintTip(x, y, r, bgPixel, 1); +} + +/* + * The pencil's dragged stroke is a hard line; it walks the pixels between + * the two points so the tip stays one square, not a long smear. + */ +static void +PencilLine(int x0, int y0, int x1, int y1, int r) +{ + int dx = x1 - x0, dy = y1 - y0; + int n = (dx < 0 ? -dx : dx) > (dy < 0 ? -dy : dy) ? + (dx < 0 ? -dx : dx) : (dy < 0 ? -dy : dy); + int i; + + if (n == 0) { + PencilDot(x1, y1, r); + return; + } + for (i = 0; i <= n; i++) + PencilDot(x0 + dx * i / n, y0 + dy * i / n, r); +} + /* ------------------------------------------------------------- the selection */ static int @@ -1697,6 +1863,58 @@ MaskClear(void) memset(selMask, 0, selMaskSize); selMaskAny = 0; hasSel = 0; + /* a hard edge again: the soft band belonged to the old shape */ + free(selCover); + selCover = NULL; + selCoverSize = 0; +} + +/* + * The coverage of one pixel: 255 inside a hard selection, 0 outside, and + * the feathered value in between. Without a coverage the bit mask is the + * answer, which keeps every operation that predates Feather unchanged. + */ +static int +MaskCover(int x, int y) +{ + size_t i; + + if (!MaskGet(x, y)) + return selCover != NULL && x >= 0 && y >= 0 && + x < docW && y < docH ? + selCover[(size_t) y * docW + x] : 0; + if (selCover == NULL) + return 255; + i = (size_t) y * docW + x; + return selCover[i]; +} + +/* the coverage buffer, one byte per pixel, all 255 where the mask is set */ +static int +CoverAlloc(void) +{ + size_t need = (size_t) docW * docH; + int x, y; + + if (selCover != NULL && selCoverSize >= need) { + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + selCover[(size_t) y * docW + x] = + (unsigned char) (MaskGet(x, y) ? 255 : 0); + return 0; + } + free(selCover); + selCover = malloc(need); + if (selCover == NULL) { + selCoverSize = 0; + return -1; + } + selCoverSize = need; + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + selCover[(size_t) y * docW + x] = + (unsigned char) (MaskGet(x, y) ? 255 : 0); + return 0; } static int @@ -1729,6 +1947,525 @@ MaskFromRect(void) for (x = selX0; x < selX1; x++) MaskSet(x, y, 1); } +/* + * Selection morphology: the distance to the nearest selected and to the + * nearest unselected pixel decides grow, contract, border and feather. + * + * The distance is exact Euclidean, not a chamfer approximation: a 3-4-5 + * chamfer is off by a few percent, and a test that checks "the pixel at + * distance 3" against a pixel that is really at 2.83 cannot be exact. + * The transform is the separable parabola one (Felzenszwalb/Huttenlocher): + * the lower envelope of parabolas per column, then per row. O(w*h). + */ +#define DIST_INF 1e20 + +static void +Dist1D(const double *f, double *d, int n, int *v, double *z) +{ + int k = 0, q; + + v[0] = 0; + z[0] = -DIST_INF; + z[1] = DIST_INF; + for (q = 1; q < n; q++) { + double s; + + do { + int p = v[k]; + + s = ((f[q] + (double) q * q) - + (f[p] + (double) p * p)) / (2.0 * q - 2.0 * p); + if (s <= z[k]) + k--; + else + break; + } while (k >= 0); + k++; + v[k] = q; + z[k] = s; + z[k + 1] = DIST_INF; + } + k = 0; + for (q = 0; q < n; q++) { + while (z[k + 1] < (double) q) + k++; + d[q] = (double) (q - v[k]) * (q - v[k]) + f[v[k]]; + } +} + +/* + * Squared distance (in pixels, exact) from every pixel to the nearest + * pixel where `ind` is set. Pixels where nothing is set get DIST_INF. + */ +static void +DistTransform(const unsigned char *ind, int w, int h, double *out) +{ + double *f, *d, *z; + int *v; + int x, y, n; + + n = w > h ? w : h; + f = malloc(sizeof(double) * (size_t) n); + d = malloc(sizeof(double) * (size_t) n); + z = malloc(sizeof(double) * (size_t) (n + 1)); + v = malloc(sizeof(int) * (size_t) n); + if (f == NULL || d == NULL || z == NULL || v == NULL) { + free(f); free(d); free(z); free(v); + for (x = 0; x < w * h; x++) + out[x] = DIST_INF; + return; + } + /* columns */ + for (x = 0; x < w; x++) { + for (y = 0; y < h; y++) + f[y] = ind[(size_t) y * w + x] ? 0.0 : DIST_INF; + Dist1D(f, d, h, v, z); + for (y = 0; y < h; y++) + out[(size_t) y * w + x] = d[y]; + } + /* rows */ + for (y = 0; y < h; y++) { + double *row = out + (size_t) y * w; + + for (x = 0; x < w; x++) + f[x] = row[x]; + Dist1D(f, d, w, v, z); + for (x = 0; x < w; x++) + row[x] = d[x]; + } + free(f); free(d); free(z); free(v); +} + +/* the mask as one byte per pixel, 1 = selected; NULL when there is none */ +static unsigned char * +MaskToBytes(int w, int h) +{ + unsigned char *b; + int x, y; + + b = calloc((size_t) w * h, 1); + if (b == NULL) + return NULL; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + b[(size_t) y * w + x] = (unsigned char) MaskGet(x, y); + return b; +} + +/* + * Put a byte field back. selMaskAny is asked from the field, and the + * marquee rectangle is the bounding box of what is left -- with an empty + * field the selection is gone and hasSel says so. + */ +static void +MaskFromBytes(const unsigned char *on, int w, int h) +{ + int x, y, x0 = w, y0 = h, x1 = -1, y1 = -1, any = 0; + + if (MaskAlloc() != 0) + return; + MaskClear(); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (on[(size_t) y * w + x]) { + MaskSet(x, y, 1); + any = 1; + if (x < x0) x0 = x; + if (y < y0) y0 = y; + if (x > x1) x1 = x; + if (y > y1) y1 = y; + } + selMaskAny = any; + if (!any) { + hasSel = 0; + return; + } + selX0 = x0; + selY0 = y0; + selX1 = x1 + 1; + selY1 = y1 + 1; + hasSel = 1; +} + +/* + * The two distance fields the commands need. `dIn` is the distance to the + * nearest selected pixel, `dOut` the distance to the nearest unselected + * one. Everything outside the picture counts as unselected, so a + * selection that runs to the edge can be contracted from there. + */ +static int +SelectionDistances(double **dInP, double **dOutP, int *wP, int *hP, + unsigned char **selP) +{ + unsigned char *sel, *inv; + double *dIn, *dOut; + size_t n; + int x, y; + + if (selMask == NULL || !selMaskAny) + return -1; + sel = MaskToBytes(docW, docH); + if (sel == NULL) + return -1; + n = (size_t) docW * docH; + inv = malloc(n); + dIn = malloc(sizeof(double) * n); + dOut = malloc(sizeof(double) * n); + if (inv == NULL || dIn == NULL || dOut == NULL) { + free(sel); free(inv); free(dIn); free(dOut); + return -1; + } + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + inv[(size_t) y * docW + x] = + (unsigned char) !sel[(size_t) y * docW + x]; + DistTransform(sel, docW, docH, dIn); + /* + * Outside the frame is unselected, so the border of the picture is a + * distance-1 neighbour of the outside. Feeding a one-pixel ring of + * "unselected" into the transform gives that without a special case + * in the loop below. + */ + { + int w2 = docW + 2, h2 = docH + 2, i; + unsigned char *pad = malloc((size_t) w2 * h2); + double *padded = malloc(sizeof(double) * (size_t) w2 * h2); + + if (pad != NULL && padded != NULL) { + memset(pad, 0, (size_t) w2 * h2); + for (y = 0; y < docH; y++) + memcpy(pad + (size_t) (y + 1) * w2 + 1, + inv + (size_t) y * docW, (size_t) docW); + DistTransform(pad, w2, h2, padded); + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + dOut[(size_t) y * docW + x] = + padded[(size_t) (y + 1) * w2 + x + 1]; + for (i = 0; i < docW * docH; i++) + if (dOut[i] == DIST_INF) + dOut[i] = DIST_INF; + } else { + DistTransform(inv, docW, docH, dOut); + } + free(pad); + free(padded); + } + free(inv); + *selP = sel; + *dInP = dIn; + *dOutP = dOut; + *wP = docW; + *hP = docH; + return 0; +} + +/* Select > Modify > Expand (grow by r pixels) */ +static void +SelExpand(double r) +{ + unsigned char *sel, *out; + double *dIn, *dOut; + int w, h, x, y; + double r2 = r * r; + + if (SelectionDistances(&dIn, &dOut, &w, &h, &sel) != 0) + return; + out = malloc((size_t) w * h); + if (out == NULL) { + free(sel); free(dIn); free(dOut); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + out[(size_t) y * w + x] = + (unsigned char) (dIn[(size_t) y * w + x] <= r2); + MaskFromBytes(out, w, h); + free(out); + free(sel); free(dIn); free(dOut); +} + +/* Select > Modify > Contract: keep what is more than r pixels from outside */ +static void +SelContract(double r) +{ + unsigned char *sel, *out; + double *dIn, *dOut; + int w, h, x, y; + double r2 = r * r; + + if (SelectionDistances(&dIn, &dOut, &w, &h, &sel) != 0) + return; + out = malloc((size_t) w * h); + if (out == NULL) { + free(sel); free(dIn); free(dOut); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + out[(size_t) y * w + x] = (unsigned char) + (sel[(size_t) y * w + x] && + dOut[(size_t) y * w + x] > r2); + MaskFromBytes(out, w, h); + free(out); + free(sel); free(dIn); free(dOut); +} + +/* + * Select > Modify > Border: a band of width r on each side of the edge, + * which is what the original draws -- the pixels inside the selection that + * are within r of the outside, plus the pixels outside within r of it. + */ +static void +SelBorder(double r) +{ + unsigned char *sel, *out; + double *dIn, *dOut; + int w, h, x, y; + double r2 = r * r; + + if (SelectionDistances(&dIn, &dOut, &w, &h, &sel) != 0) + return; + out = malloc((size_t) w * h); + if (out == NULL) { + free(sel); free(dIn); free(dOut); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + size_t i = (size_t) y * w + x; + + out[i] = (unsigned char) + ((sel[i] && dOut[i] <= r2) || + (!sel[i] && dIn[i] <= r2)); + } + MaskFromBytes(out, w, h); + free(out); + free(sel); free(dIn); free(dOut); +} + +/* + * Select > Modify > Smooth: a majority vote in a (2r+1) square. A pixel + * flips when more than half of its neighbourhood disagrees with it, which + * is what rounds a corner and removes a one-pixel speck without moving an + * edge. The vote runs on summed-area tables, so the cost is the picture + * size and not the radius. + */ +static void +SelSmooth(int r) +{ + unsigned char *sel, *out; + long *sum; + int w, h, x, y, side, i; + + if (selMask == NULL || !selMaskAny || r < 1) + return; + w = docW; + h = docH; + sel = MaskToBytes(w, h); + if (sel == NULL) + return; + out = malloc((size_t) w * h); + sum = malloc(sizeof(long) * (size_t) (w + 1) * (h + 1)); + if (out == NULL || sum == NULL) { + free(sel); free(out); free(sum); + return; + } + for (i = 0; i < (w + 1) * (h + 1); i++) + sum[i] = 0; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + sum[(size_t) (y + 1) * (w + 1) + (x + 1)] = + (sel[(size_t) y * w + x] ? 1 : 0) + + sum[(size_t) y * (w + 1) + (x + 1)] + + sum[(size_t) (y + 1) * (w + 1) + x] - + sum[(size_t) y * (w + 1) + x]; + side = 2 * r + 1; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int x0 = x - r < 0 ? 0 : x - r; + int y0 = y - r < 0 ? 0 : y - r; + int x1 = x + r >= w ? w - 1 : x + r; + int y1 = y + r >= h ? h - 1 : y + r; + long on = sum[(size_t) (y1 + 1) * (w + 1) + (x1 + 1)] - + sum[(size_t) y0 * (w + 1) + (x1 + 1)] - + sum[(size_t) (y1 + 1) * (w + 1) + x0] + + sum[(size_t) y0 * (w + 1) + x0]; + long cnt = (long) (x1 - x0 + 1) * (y1 - y0 + 1); + + (void) side; + out[(size_t) y * w + x] = + (unsigned char) (on * 2 > cnt); + } + MaskFromBytes(out, w, h); + free(out); + free(sum); + free(sel); +} + +/* + * Select > Grow and Select > Similar: the colours of the current selection + * are the reference, and everything within the tolerance joins. + * + * Grow walks out from the selection (connected), Similar takes the whole + * picture (not connected) -- that is the difference between the two in the + * original. The reference colour is the mean of the selection, which is + * the same answer for a flat area and a defensible one for a mixed one; + * the wand's own click colour cannot be used here, it is gone by then. + */ +static void +SelColorExtend(int tolerance, int connected) +{ + unsigned char *sel, *out, *rgb = NULL; + unsigned char ref[3]; + long sr = 0, sg = 0, sb = 0, cnt = 0; + int w, h, x, y; + + if (selMask == NULL || !selMaskAny) + return; + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + sel = MaskToBytes(w, h); + if (sel == NULL) { + free(rgb); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (sel[(size_t) y * w + x]) { + unsigned char *d = rgb + + ((size_t) y * w + x) * 3; + + sr += d[0]; sg += d[1]; sb += d[2]; + cnt++; + } + if (cnt == 0) { + free(sel); + free(rgb); + return; + } + ref[0] = (unsigned char) (sr / cnt); + ref[1] = (unsigned char) (sg / cnt); + ref[2] = (unsigned char) (sb / cnt); + out = malloc((size_t) w * h); + if (out == NULL) { + free(sel); + free(rgb); + return; + } + memcpy(out, sel, (size_t) w * h); + if (connected) { + int *stack = malloc(sizeof(int) * (size_t) w * h); + int sp = 0; + + if (stack == NULL) { + free(out); free(sel); free(rgb); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (out[(size_t) y * w + x]) + stack[sp++] = y * w + x; + while (sp > 0) { + int p = stack[--sp], px = p % w, py = p / w; + int nb[4], k; + + nb[0] = px > 0 ? p - 1 : -1; + nb[1] = px < w - 1 ? p + 1 : -1; + nb[2] = py > 0 ? p - w : -1; + nb[3] = py < h - 1 ? p + w : -1; + for (k = 0; k < 4; k++) { + int q = nb[k], dr, dg, db; + unsigned char *d; + + if (q < 0 || out[q]) + continue; + d = rgb + (size_t) q * 3; + dr = (int) d[0] - ref[0]; + dg = (int) d[1] - ref[1]; + db = (int) d[2] - ref[2]; + if (dr < 0) dr = -dr; + if (dg < 0) dg = -dg; + if (db < 0) db = -db; + if (dr > tolerance || dg > tolerance || + db > tolerance) + continue; + out[q] = 1; + stack[sp++] = q; + } + } + free(stack); + } else { + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + size_t i = (size_t) y * w + x; + unsigned char *d = rgb + i * 3; + int dr, dg, db; + + if (out[i]) + continue; + dr = (int) d[0] - ref[0]; + dg = (int) d[1] - ref[1]; + db = (int) d[2] - ref[2]; + if (dr < 0) dr = -dr; + if (dg < 0) dg = -dg; + if (db < 0) db = -db; + if (dr > tolerance || dg > tolerance || + db > tolerance) + continue; + out[i] = 1; + } + } + MaskFromBytes(out, w, h); + free(out); + free(sel); + free(rgb); +} + +/* + * Select > Feather: r pixels of soft edge, inside the selection only. + * + * The coverage is the distance from the edge, taken from the outside + * distance field and ramped over r pixels; 255 stays in the middle. A + * box blur would have been cheaper to write and wrong at the corners -- + * it smears horizontally and vertically by different amounts, and it + * bleeds out of the selection, which Feather does not do. + */ +static void +SelFeather(int r) +{ + unsigned char *sel; + double *dIn, *dOut; + int w, h, x, y; + + if (r < 1) + return; + if (SelectionDistances(&dIn, &dOut, &w, &h, &sel) != 0) + return; + (void) dIn; + if (CoverAlloc() != 0) { + free(sel); free(dIn); free(dOut); + return; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + size_t i = (size_t) y * w + x; + double dist = dOut[i] > 0.0 ? dOut[i] : 0.0; + double t; + + if (!sel[i]) { + selCover[i] = 0; + continue; + } + t = (dist - 1.0) / (double) r; + if (t < 0.0) + t = 0.0; + if (t > 1.0) + t = 1.0; + /* a straight ramp: 0 at the edge, 255 in the middle */ + selCover[i] = (unsigned char) (t * 255.0 + 0.5); + } + free(sel); free(dIn); free(dOut); +} /* * The lasso and the magic wand both end with a set of pixels; this turns @@ -2105,9 +2842,30 @@ PaintMask(Pixel px) if (im == NULL) return; for (y = 0; y < docH; y++) - for (x = 0; x < docW; x++) - if (MaskGet(x, y)) + for (x = 0; x < docW; x++) { + int cov = MaskCover(x, y); + + if (cov <= 0) + continue; + if (cov >= 255) { XPutPixel(im, x, y, px); + continue; + } + /* the soft band mixes the paint with the pixel */ + { + unsigned long old = XGetPixel(im, x, y); + unsigned char o[3], n[3], mix[3]; + int c; + + PixelToRGB(old, &o[0], &o[1], &o[2]); + PixelToRGB(px, &n[0], &n[1], &n[2]); + for (c = 0; c < 3; c++) + mix[c] = (unsigned char) (o[c] + + ((int) n[c] - o[c]) * cov / 255); + XPutPixel(im, x, y, RGBToPixel(mix[0], + mix[1], mix[2])); + } + } XPutImage(dpy, image, gc, im, 0, 0, 0, 0, (unsigned int) docW, (unsigned int) docH); XDestroyImage(im); @@ -2283,6 +3041,10 @@ MoveMasked(int dx, int dy) selMask = fresh; selMaskSize = sz; selMaskAny = any; + /* a new shape: the old soft band does not fit it */ + free(selCover); + selCover = NULL; + selCoverSize = 0; /* the marquee follows the moved block */ selX0 = (selX0 + dx < 0) ? 0 : selX0 + dx; selY0 = (selY0 + dy < 0) ? 0 : selY0 + dy; @@ -2363,17 +3125,48 @@ typedef void (*ChanFn)(unsigned char *p, void *cd); static void OpPerPixel(ChanFn fn, void *cd) { - unsigned char *rgb; - int w, h, x, y; + unsigned char *rgb, *before; + int w, h, x, y, cov; if (GetImageRGB(&rgb, &w, &h) != 0) return; + before = malloc((size_t) w * h * 3); + if (before != NULL) + memcpy(before, rgb, (size_t) w * h * 3); for (y = 0; y < h; y++) for (x = 0; x < w; x++) { - if (selMaskAny && !MaskGet(x, y)) + unsigned char *p = rgb + ((size_t) y * w + x) * 3; + + if (!selMaskAny) { + fn(p, cd); continue; - fn(rgb + ((size_t) y * w + x) * 3, cd); + } + cov = MaskCover(x, y); + if (cov <= 0) + continue; + if (cov >= 255) { + fn(p, cd); + continue; + } + /* + * A feathered edge mixes the correction with what was + * there: a hard cut at a soft edge would put the band + * back the feather just removed. + */ + fn(p, cd); + if (before != NULL) { + const unsigned char *o = + before + ((size_t) y * w + x) * 3; + + p[0] = (unsigned char) (o[0] + + ((int) p[0] - o[0]) * cov / 255); + p[1] = (unsigned char) (o[1] + + ((int) p[1] - o[1]) * cov / 255); + p[2] = (unsigned char) (o[2] + + ((int) p[2] - o[2]) * cov / 255); + } } + free(before); SetImageRGB(rgb, w, h); free(rgb); } @@ -2875,19 +3668,44 @@ GradientImage(int x0, int y0, int x1, int y1) for (y = 0; y < h; y++) for (x = 0; x < w; x++) { long t; + int cov = 255; unsigned char *d; - if (selMaskAny && !MaskGet(x, y)) - continue; - /* how far along the drag, clamped to the ends */ - t = ((long) (x - x0) * dx + (long) (y - y0) * dy) * - 1000 / len2; + if (selMaskAny && docW == w && docH == h) { + cov = MaskCover(x, y); + if (cov <= 0) + continue; + } + if (gradientKind == 1) { + /* radial: the distance from where the drag + * started, against the length of the drag */ + long ex = x - x0, ey = y - y0; + long d2 = ex * ex + ey * ey; + + t = (long) (sqrt((double) d2) * + 1000.0 / sqrt((double) len2)); + } else { + /* linear: how far along the drag */ + t = ((long) (x - x0) * dx + + (long) (y - y0) * dy) * 1000 / len2; + } if (t < 0) t = 0; if (t > 1000) t = 1000; d = rgb + ((size_t) y * w + x) * 3; - d[0] = (unsigned char) (fr + (tr - fr) * t / 1000); - d[1] = (unsigned char) (fg + (tg - fg) * t / 1000); - d[2] = (unsigned char) (fb + (tb - fb) * t / 1000); + { + unsigned char nr, ng, nb; + + nr = (unsigned char) (fr + (tr - fr) * t / 1000); + ng = (unsigned char) (fg + (tg - fg) * t / 1000); + nb = (unsigned char) (fb + (tb - fb) * t / 1000); + /* a feathered edge mixes with what was there */ + d[0] = (unsigned char) (d[0] + + ((int) nr - d[0]) * cov / 255); + d[1] = (unsigned char) (d[1] + + ((int) ng - d[1]) * cov / 255); + d[2] = (unsigned char) (d[2] + + ((int) nb - d[2]) * cov / 255); + } } SetImageRGB(rgb, w, h); free(rgb); @@ -2994,6 +3812,77 @@ MosaicOk(int cell, int unused) fprintf(stderr, "dtphoto: mosaic %d\n", cell); } +/* Select > Modify: one radius, in pixels */ +static void +FeatherOk(int r, int unused) +{ + (void) unused; + SelFeather(r); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: feather %d\n", r); +} + +static void +SelBorderOk(int r, int unused) +{ + (void) unused; + SelBorder((double) (r < 1 ? 1 : r)); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: border %d\n", r); +} + +static void +SelSmoothOk(int r, int unused) +{ + (void) unused; + SelSmooth(r); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: smooth %d\n", r); +} + +static void +SelExpandOk(int r, int unused) +{ + (void) unused; + if (r < 1) + return; + SelExpand((double) r); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: expand %d\n", r); +} + +static void +SelContractOk(int r, int unused) +{ + (void) unused; + if (r < 1) + return; + SelContract((double) r); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: contract %d\n", r); +} + +/* + * Grow and Similar take the tolerance from the wand's own value, which is + * what the original does -- the two choices live in the same Options box + * and a second field would only let them drift apart. + */ +static void +SelGrowRun(void) +{ + SelColorExtend(wandTolerance, 1); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: grow %d\n", wandTolerance); +} + +static void +SelSimilarRun(void) +{ + SelColorExtend(wandTolerance, 0); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: similar %d\n", wandTolerance); +} + /* one line for a tool or a menu action, so a mouse test can read it */ static void OpDone(const char *what) @@ -3086,7 +3975,7 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) LassoAdd(docx, docy); break; case 3: /* magic wand: the colour under the pointer */ - WandSelect(docx, docy, 32); + WandSelect(docx, docy, wandTolerance); dragging = 0; break; case 4: /* move: drag the masked pixels */ @@ -3109,15 +3998,14 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) TypeIntoImage(docx, docy); dragging = 0; break; - case 10: /* pencil */ - XDrawPoint(dpy, image, gc, docx, docy); + case 10: /* pencil: a hard tip */ + PencilDot(docx, docy, pencilRadius); break; - case 11: /* brush */ - Dab(docx, docy, 3); + case 11: /* brush: round and soft */ + BrushDot(docx, docy, pencilRadius); break; - case 12: /* eraser */ - XSetForeground(dpy, gc, bgPixel); - Dab(docx, docy, 6); + case 12: /* eraser paints the background */ + EraserDot(docx, docy, eraserRadius); break; case 13: /* bucket */ FloodFillImage(docx, docy); @@ -3129,7 +4017,8 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) case 15: case 16: case 17: case 18: case 19: case 20: /* the retouching tools */ smudgeValid = 0; - Retouch(docx, docy, 6, 60, currentTool); + Retouch(docx, docy, retouchRadius, + retouchStrength, currentTool); break; case 21: /* eyedropper */ { @@ -3174,15 +4063,14 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) lastScrX = ev->xmotion.x; lastScrY = ev->xmotion.y; break; - case 10: - LineImage(lastX, lastY, docx, docy); + case 10: /* pencil: a hard stroke */ + PencilLine(lastX, lastY, docx, docy, pencilRadius); break; - case 11: - Dab(docx, docy, 3); + case 11: /* brush: a soft round dab */ + BrushDot(docx, docy, pencilRadius); break; - case 12: - XSetForeground(dpy, gc, bgPixel); - Dab(docx, docy, 6); + case 12: /* eraser: the background colour */ + EraserDot(docx, docy, eraserRadius); break; case 9: case 13: XCopyArea(dpy, snap, image, gc, 0, 0, docW, docH, 0, 0); @@ -3196,7 +4084,8 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) break; case 15: case 16: case 17: case 18: case 19: case 20: - Retouch(docx, docy, 6, 60, currentTool); + Retouch(docx, docy, retouchRadius, + retouchStrength, currentTool); break; default: break; @@ -3366,6 +4255,161 @@ DrawGlyph(Display *d, Pixmap p, GC gc, int tool) XFreeGC(d, white); } +/* + * The Options box. Photoshop 3 has one and it is where every brush size, + * the wand's tolerance and the gradient's shape live; before this box the + * numbers were constants in the event handler, so a 3 px pencil was all the + * program could do. + * + * Every field writes straight into the variable the tool reads, so there is + * no second copy to keep in step. The box is built once and shown on + * request; closing it hides it and keeps the values. + */ +static Widget optionsShell; +static Widget optPencil, optEraser, optWand, optRetouch, optStrength; + +static void +OptionsApply(Widget w, XtPointer cd, XtPointer call) +{ + const char *s; + int *target = (int *) cd; + char *p = NULL; + int v; + + (void) call; + XtVaGetValues(w, XmNvalue, &s, NULL); + if (s == NULL || *s == '\0') + return; + v = (int) strtol(s, &p, 10); + if (p == s) + return; + *target = v; + fprintf(stderr, "dtphoto: options %s=%d\n", + (target == &pencilRadius) ? "pencil" : + (target == &eraserRadius) ? "eraser" : + (target == &wandTolerance) ? "tolerance" : + (target == &retouchRadius) ? "retouch" : "strength", v); +} + +/* + * One row: a label on the left and a field on the right. Both hang off + * `above`, and the row returned is the FIELD, not the label: a text field + * is taller than its label, so attaching the next row to the label put the + * rows on top of each other and a click landed in the field above + * (measured 2026-11-22: the tolerance field was unreachable). + */ +static Widget +OptField(Widget form, Widget above, const char *label, const char *name, + int value, int *target) +{ + Widget lab, field; + char buf[16]; + + snprintf(buf, sizeof buf, "%d", value); + lab = XtVaCreateManagedWidget(name, xmLabelWidgetClass, form, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, above, + XmNleftAttachment, XmATTACH_FORM, XmNalignment, + XmALIGNMENT_BEGINNING, XmNrecomputeSize, False, XmNwidth, 90, + XmNlabelString, XmStringCreateLocalized(label), NULL); + field = XtVaCreateManagedWidget("field", xmTextFieldWidgetClass, form, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, above, + XmNleftAttachment, XmATTACH_WIDGET, XmNleftWidget, lab, + XmNrightAttachment, XmATTACH_FORM, XmNvalue, buf, NULL); + XtAddCallback(field, XmNactivateCallback, OptionsApply, + (XtPointer) target); + XtAddCallback(field, XmNfocusCallback, OptionsApply, (XtPointer) target); + return field; +} + +static void +OptionsClose(Widget w, XtPointer cd, XtPointer call) +{ + (void) w; (void) cd; (void) call; + if (optionsShell != NULL) + XtPopdown(optionsShell); +} + +/* linear (0) or radial (1): the gradient tool reads this on the next drag */ +static void +GradientKindCallback(Widget w, XtPointer cd, XtPointer call) +{ + (void) call; + if (XmToggleButtonGetState(w)) { + gradientKind = (int) (long) cd; + fprintf(stderr, "dtphoto: gradient %s\n", + gradientKind == 0 ? "linear" : "radial"); + } +} + +static void +BuildOptions(Widget parent) +{ + Widget form, close, grad; + Widget last; + + form = XtVaCreateManagedWidget("options", xmFormWidgetClass, parent, + XmNmarginWidth, 6, XmNmarginHeight, 6, NULL); + last = XtVaCreateManagedWidget("opttitle", xmLabelWidgetClass, form, + XmNtopAttachment, XmATTACH_FORM, XmNleftAttachment, XmATTACH_FORM, + XmNalignment, XmALIGNMENT_BEGINNING, XmNlabelString, + XmStringCreateLocalized(Tr(6, "Tool Options")), NULL); + last = OptField(form, last, "Pencil radius:", "pencil", + pencilRadius, &pencilRadius); + last = OptField(form, last, "Eraser radius:", "eraser", + eraserRadius, &eraserRadius); + last = OptField(form, last, "Tolerance:", "wand", + wandTolerance, &wandTolerance); + last = OptField(form, last, "Retouch radius:", "retouch", + retouchRadius, &retouchRadius); + last = OptField(form, last, "Retouch strength:", "strength", + retouchStrength, &retouchStrength); + + grad = XtVaCreateManagedWidget("gradient", xmRowColumnWidgetClass, form, + XmNtopAttachment, XmATTACH_WIDGET, XmNtopWidget, last, + XmNleftAttachment, XmATTACH_FORM, XmNrightAttachment, + XmATTACH_FORM, XmNorientation, XmHORIZONTAL, XmNradioBehavior, + True, XmNpacking, XmPACK_TIGHT, XmNmarginHeight, 2, NULL); + { + Widget b; + + b = XtVaCreateManagedWidget("linear", xmToggleButtonWidgetClass, + grad, XmNindicatorType, XmONE_OF_MANY, + XmNlabelString, XmStringCreateLocalized("Linear"), NULL); + XmToggleButtonSetState(b, gradientKind == 0, False); + XtAddCallback(b, XmNvalueChangedCallback, GradientKindCallback, + (XtPointer) 0L); + b = XtVaCreateManagedWidget("radial", xmToggleButtonWidgetClass, + grad, XmNindicatorType, XmONE_OF_MANY, + XmNlabelString, XmStringCreateLocalized("Radial"), NULL); + XmToggleButtonSetState(b, gradientKind == 1, False); + XtAddCallback(b, XmNvalueChangedCallback, GradientKindCallback, + (XtPointer) 1L); + } + + close = XtVaCreateManagedWidget("close", xmPushButtonWidgetClass, form, + XmNleftAttachment, XmATTACH_FORM, XmNbottomAttachment, + XmATTACH_FORM, XmNlabelString, + XmStringCreateLocalized("Close"), NULL); + XtAddCallback(close, XmNactivateCallback, OptionsClose, NULL); + optPencil = form; + (void) optEraser; (void) optWand; (void) optRetouch; (void) optStrength; +} + +static void +OptionsShow(void) +{ + if (optionsShell == NULL) { + optionsShell = XtVaAppCreateShell("options", "Dtphoto", + topLevelShellWidgetClass, dpy, XmNtitle, + Tr(6, "Tool Options"), XmNallowShellResize, True, + XmNx, 120, XmNy, 64, NULL); + BuildOptions(optionsShell); + XtRealizeWidget(optionsShell); + } + XtPopup(optionsShell, XtGrabNone); + fprintf(stderr, "dtphoto: options shown\n"); +} + static void ToolCallback(Widget w, XtPointer data, XtPointer call) { @@ -3434,7 +4478,7 @@ FileOk(Widget w, XtPointer data, XtPointer call) path != NULL) { int save = fileSave; - if (save ? (SaveBMP(path) == 0) : (OpenBMP(path) == 0)) { + if (save ? (SaveImage(path) == 0) : (OpenImage(path) == 0)) { fprintf(stderr, "dtphoto: %s %s\n", save ? "saved" : "opened", path); UpdateStatus(); @@ -3466,11 +4510,21 @@ static void FileDialog(int save) { Widget d = XmCreateFileSelectionDialog(top, "file", NULL, 0); + XmString pat; fileSave = save; XtAddCallback(d, XmNokCallback, FileOk, NULL); XtAddCallback(d, XmNcancelCallback, FileCancel, NULL); XtVaSetValues(d, XmNfileTypeMask, XmFILE_REGULAR, NULL); + /* + * The filter has to name every format this program reads, or opening + * a PNG means typing its name: the reader knows more than the writer + * did in 1993 (2026-11-22). + */ + pat = XmStringCreateLocalized("*.png *.jpg *.jpeg *.gif *.bmp *.ico " + "*.xpm *"); + XtVaSetValues(d, XmNpattern, pat, NULL); + XmStringFree(pat); XtManageChild(d); } @@ -3541,6 +4595,9 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) XCopyArea(dpy, snap, image, gc, 0, 0, docW, docH, 0, 0); RenderDoc(canvas); break; + case CMD_SHOWOPTIONS: + OptionsShow(); + break; case CMD_ZOOMIN: SetZoom(zoom * 2); break; case CMD_ZOOMOUT: SetZoom(zoom / 2); break; case CMD_FIT: { @@ -3566,6 +4623,31 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) InvertSelection(); RenderDoc(canvas); break; + case CMD_FEATHER: { + char buf[32]; + + snprintf(buf, sizeof buf, "%d", retouchRadius); + NumberDialog("Feather radius (pixels):", buf, FeatherOk); + break; + } + case CMD_SELBORDER: + NumberDialog("Border width (pixels):", "4", SelBorderOk); + break; + case CMD_SELSMOOTH: + NumberDialog("Smooth radius (pixels):", "2", SelSmoothOk); + break; + case CMD_SELEXPAND: + NumberDialog("Expand by (pixels):", "4", SelExpandOk); + break; + case CMD_SELCONTRACT: + NumberDialog("Contract by (pixels):", "4", SelContractOk); + break; + case CMD_SELGROW: + SelGrowRun(); + break; + case CMD_SELSIMILAR: + SelSimilarRun(); + break; case CMD_CLEAR: XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); PaintMask(bgPixel); @@ -3743,6 +4825,11 @@ main(int argc, char **argv) XtRealizeWidget(toolShell); XtPopup(toolShell, XtGrabNone); + /* the Options box is a request, normally from Window/Show Options; + * this makes it reachable without a mouse, for the tests */ + if (getenv("DTPHOTO_OPTIONS") != NULL) + OptionsShow(); + NewDoc(); /* @@ -3753,12 +4840,12 @@ main(int argc, char **argv) XImage *a, *b; int x, y, ok = 1; - if (OpenBMP(argv[2]) != 0 || SaveBMP(argv[3]) != 0) { + if (OpenImage(argv[2]) != 0 || SaveImage(argv[3]) != 0) { printf("dtphoto selftest: FAIL open/save\n"); return 1; } a = XGetImage(dpy, snap, 0, 0, docW, docH, AllPlanes, ZPixmap); - if (OpenBMP(argv[3]) != 0) { + if (OpenImage(argv[3]) != 0) { printf("dtphoto selftest: FAIL reopen\n"); return 1; } @@ -4220,6 +5307,13 @@ main(int argc, char **argv) { "Select/All", CMD_ALL }, { "Select/None", CMD_DESELECT }, { "Select/Inverse", CMD_INVERSE }, + { "Select/Feather...", CMD_FEATHER }, + { "Select/Modify/Border...", CMD_SELBORDER }, + { "Select/Modify/Smooth...", CMD_SELSMOOTH }, + { "Select/Modify/Expand...", CMD_SELEXPAND }, + { "Select/Modify/Contract...", CMD_SELCONTRACT }, + { "Select/Grow", CMD_SELGROW }, + { "Select/Similar", CMD_SELSIMILAR }, { "Image/Adjust/Invert", CMD_INVERT }, { "Image/Adjust/Desaturate", CMD_DESATURATE }, { "Image/Adjust/Brightness/Contrast...", CMD_BRIGHTCONTRAST }, @@ -4272,6 +5366,289 @@ main(int argc, char **argv) * known corner. A wrong one of these is a wrong picture, not a bug * that shows later. */ + if (argc >= 2 && strcmp(argv[1], "-selmodtest") == 0) { + /* + * Selection morphology, against numbers worked out by hand. + * + * The document is 21x11 with a 5x5 block at (8,3)..(12,7), + * so every distance below is a small integer and the exact + * Euclidean transform has to land on it -- a chamfer would + * not, and the test would say so. + */ + int w = 21, h = 11, bad = 0, x, y; + unsigned char *rgb; + int n; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto selmodtest: FAIL malloc\n"); + return 1; + } + for (n = 0; n < w * h; n++) { + rgb[n * 3] = 10; + rgb[n * 3 + 1] = 20; + rgb[n * 3 + 2] = 30; + } + SetImageRGB(rgb, w, h); + free(rgb); + +#define SELMOD_BLOCK() \ + do { \ + int bx, by; \ + MaskClear(); \ + if (MaskAlloc() != 0) { \ + printf("dtphoto selmodtest: FAIL alloc\n"); \ + return 1; \ + } \ + selMaskAny = 1; \ + for (by = 3; by <= 7; by++) \ + for (bx = 8; bx <= 12; bx++) \ + MaskSet(bx, by, 1); \ + hasSel = 1; \ + selX0 = 8; selY0 = 3; selX1 = 13; selY1 = 8; \ + } while (0) + +#define SELMOD_WANT(name, px, py, want) \ + do { \ + int got = MaskGet(px, py); \ + if (got != (want)) { \ + printf("dtphoto selmodtest: %s (%d,%d)=%d want %d " \ + "FAIL\n", name, px, py, got, (want)); \ + bad++; \ + } \ + } while (0) + + /* --- Expand by 2: the block plus a 2 px ring --- */ + SELMOD_BLOCK(); + SelExpand(2.0); + SELMOD_WANT("expand", 6, 5, 1); /* 2 left of the block */ + SELMOD_WANT("expand", 8, 5, 1); + SELMOD_WANT("expand", 5, 5, 0); /* 3 left: outside */ + SELMOD_WANT("expand", 10, 1, 1); /* 2 above */ + SELMOD_WANT("expand", 10, 0, 0); /* 3 above: outside */ + SELMOD_WANT("expand", 8, 3, 1); /* a corner stays in */ + /* the ring is round, not square: (6,1) is 5.66 away */ + SELMOD_WANT("expand-corner", 6, 1, 0); + SELMOD_WANT("expand-corner", 7, 2, 1); /* 2.83 away, in */ + + /* --- Contract by 1: the block loses its rim --- */ + SELMOD_BLOCK(); + SelContract(1.0); + SELMOD_WANT("contract", 8, 3, 0); /* rim: 1 away */ + SELMOD_WANT("contract", 9, 4, 1); /* 2 away: stays */ + SELMOD_WANT("contract", 10, 5, 1); /* centre stays */ + SELMOD_WANT("contract", 9, 5, 1); /* 2 away */ + + /* --- Contract by 2: a 5x5 block leaves one pixel --- */ + SELMOD_BLOCK(); + SelContract(2.0); + SELMOD_WANT("contract2", 10, 5, 1); + SELMOD_WANT("contract2", 9, 4, 0); + + /* --- Border 1: the rim only, inside and outside --- */ + SELMOD_BLOCK(); + SelBorder(1.0); + SELMOD_WANT("border", 7, 5, 1); /* one outside */ + SELMOD_WANT("border", 8, 5, 1); /* one inside */ + SELMOD_WANT("border", 9, 5, 0); /* two inside: centre */ + SELMOD_WANT("border", 10, 5, 0); + SELMOD_WANT("border", 6, 5, 0); /* two outside */ + + /* --- Smooth 1: a lone pixel disappears, a block stays --- */ + MaskClear(); + if (MaskAlloc() != 0) + return 1; + selMaskAny = 1; + MaskSet(2, 2, 1); + SelSmooth(1); + SELMOD_WANT("smooth-speck", 2, 2, 0); + SELMOD_BLOCK(); + { + int before = 0, after = 0; + + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + before += MaskGet(x, y); + SelSmooth(1); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + after += MaskGet(x, y); + /* + * A 5x5 block: the four corners see 3 of 9 set + * pixels, so they leave; the rim between them sees + * 6 and stays. 25 -> 21, worked out on paper. + */ + if (before != 25 || after != 21) { + printf("dtphoto selmodtest: smooth-block " \ + "%d->%d want 25->21 FAIL\n", before, after); + bad++; + } + SELMOD_WANT("smooth-corner", 8, 3, 0); + SELMOD_WANT("smooth-rim", 9, 3, 1); + } + + /* + * --- Grow vs Similar --- + * + * Two 3x3 squares of the same colour at (2,2) and (14,2), + * nothing between. Grow, started from the left one, must + * reach only the pixels of that square: the right one is not + * connected. Similar takes both. + */ + { + unsigned char *pic = malloc((size_t) w * h * 3); + + if (pic == NULL) + return 1; + for (n = 0; n < w * h; n++) { + pic[n * 3] = 200; + pic[n * 3 + 1] = 200; + pic[n * 3 + 2] = 200; + } + for (y = 2; y <= 4; y++) + for (x = 2; x <= 4; x++) { + pic[((size_t) y * w + x) * 3] = 0; + pic[((size_t) y * w + x) * 3 + 1] = 0; + pic[((size_t) y * w + x) * 3 + 2] = 0; + } + for (y = 2; y <= 4; y++) + for (x = 14; x <= 16; x++) { + pic[((size_t) y * w + x) * 3] = 0; + pic[((size_t) y * w + x) * 3 + 1] = 0; + pic[((size_t) y * w + x) * 3 + 2] = 0; + } + SetImageRGB(pic, w, h); + free(pic); + + MaskClear(); + if (MaskAlloc() != 0) + return 1; + selMaskAny = 1; + MaskSet(3, 3, 1); + wandTolerance = 32; + SelColorExtend(wandTolerance, 1); + SELMOD_WANT("grow-left", 2, 2, 1); + SELMOD_WANT("grow-left", 4, 4, 1); + SELMOD_WANT("grow-right", 15, 3, 0); + + MaskClear(); + if (MaskAlloc() != 0) + return 1; + selMaskAny = 1; + MaskSet(3, 3, 1); + SelColorExtend(wandTolerance, 0); + SELMOD_WANT("similar-left", 2, 2, 1); + SELMOD_WANT("similar-right", 15, 3, 1); + SELMOD_WANT("similar-white", 10, 8, 0); + } + + /* + * --- Feather --- + * + * A 9x9 block feathered by 4 has to ramp: the rim pixel gets + * a small coverage, the centre 255, and nothing outside is + * touched. The exact numbers come from the ramp + * (dist-1)/r, so they are arithmetic, not taste. + */ + { + int c0, c1, c2, out; + + MaskClear(); + if (MaskAlloc() != 0) + return 1; + selMaskAny = 1; + for (y = 1; y <= 9; y++) + for (x = 1; x <= 9; x++) + MaskSet(x, y, 1); + hasSel = 1; + SelFeather(4); + c0 = MaskCover(5, 5); /* centre: 255 */ + c1 = MaskCover(1, 5); /* rim: dist 1 -> 0 */ + c2 = MaskCover(5, 1); /* dist 1 -> 0 */ + out = MaskCover(0, 5); /* outside the block */ + if (c0 != 255) { + printf("dtphoto selmodtest: feather-centre=%d " \ + "want 255 FAIL\n", c0); + bad++; + } + if (c1 != 0 || c2 != 0) { + printf("dtphoto selmodtest: feather-rim=%d,%d " \ + "want 0,0 FAIL\n", c1, c2); + bad++; + } + if (out != 0) { + printf("dtphoto selmodtest: feather-out=%d " \ + "want 0 FAIL\n", out); + bad++; + } + /* the ramp must be monotone and reach the middle */ + if (!(MaskCover(2, 5) <= MaskCover(3, 5) && + MaskCover(3, 5) <= MaskCover(4, 5) && + MaskCover(4, 5) <= MaskCover(5, 5))) { + printf("dtphoto selmodtest: feather ramp not " \ + "monotone FAIL\n"); + bad++; + } + /* + * And it must reach the pixel operation: Invert with + * a feathered edge has to leave the rim nearly alone + * and turn the centre completely. + */ + { + unsigned char *now = NULL; + int nw = 0, nh = 0, k; + + /* the Similar block above replaced the picture */ + rgb = malloc((size_t) w * h * 3); + if (rgb != NULL) { + for (k = 0; k < w * h; k++) { + rgb[k * 3] = 10; + rgb[k * 3 + 1] = 20; + rgb[k * 3 + 2] = 30; + } + SetImageRGB(rgb, w, h); + free(rgb); + } + OpInvert(); + GetImageRGB(&now, &nw, &nh); + if (now != NULL) { + unsigned char *mid = now + + ((size_t) 5 * nw + 5) * 3; + unsigned char *rim = now + + ((size_t) 5 * nw + 1) * 3; + + /* centre: 10,20,30 -> 245,235,225 */ + if (mid[0] != 245 || mid[1] != 235 || + mid[2] != 225) { + printf("dtphoto selmodtest: " \ + "feather-invert mid=%d,%d," \ + "%d want 245,235,225 " \ + "FAIL\n", mid[0], mid[1], + mid[2]); + bad++; + } + /* rim: coverage 0, so untouched */ + if (rim[0] != 10 || rim[1] != 20 || + rim[2] != 30) { + printf("dtphoto selmodtest: " \ + "feather-invert rim=%d,%d," \ + "%d want 10,20,30 " \ + "FAIL\n", rim[0], rim[1], + rim[2]); + bad++; + } + free(now); + } + } + } + +#undef SELMOD_BLOCK +#undef SELMOD_WANT + MaskClear(); + printf("dtphoto selmodtest: %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; @@ -4398,6 +5775,91 @@ main(int argc, char **argv) OPT_PIX("gradient", 5, 0, 255, 255, 255); OPT_PIX("gradient", 5, 3, 255, 255, 255); + /* + * The radial gradient: the drag is 5 px to the right, so + * (0,0) is the foreground, (5,3) is past the end and takes + * the background, and (3,0) is 3/5 of the way there: + * 0 + 255*600/1000 = 153. + */ + OPT_LOAD(); + gradientKind = 1; + GradientImage(0, 0, 5, 0); + OPT_PIX("radial", 0, 0, 0, 0, 0); + OPT_PIX("radial", 3, 0, 153, 153, 153); + OPT_PIX("radial", 5, 3, 255, 255, 255); + gradientKind = 0; + + /* the gradient must stay inside a selection */ + OPT_LOAD(); + MaskAlloc(); + MaskClear(); + MaskSet(0, 0, 1); + MaskSet(1, 0, 1); + selMaskAny = 1; + GradientImage(0, 0, w - 1, 0); + OPT_PIX("gradient-sel", 0, 0, 0, 0, 0); + OPT_PIX("gradient-sel", 5, 3, 200, 180, 128); + /* + * The wand reads wandTolerance, the number the Options box + * writes: at 0 it takes only the exact colour, at 100 the + * neighbouring step as well. + */ + OPT_LOAD(); + wandTolerance = 0; + WandSelect(0, 0, wandTolerance); + int optInside = 0; + { + int xx, yy; + + for (yy = 0; yy < 4; yy++) + for (xx = 0; xx < 6; xx++) + optInside += MaskGet(xx, yy) ? 1 : 0; + } + printf("dtphoto optest: wand tol 0 -> %d px\n", optInside); + MaskClear(); + selMaskAny = 0; + wandTolerance = 100; + WandSelect(0, 0, wandTolerance); + { + int xx, yy, wide = 0; + + for (yy = 0; yy < 4; yy++) + for (xx = 0; xx < 6; xx++) + wide += MaskGet(xx, yy) ? 1 : 0; + printf("dtphoto optest: wand tol 100 -> %d px %s\n", + wide, wide > optInside ? "ok" : "FAIL"); + if (wide <= optInside) + bad++; + } + MaskClear(); + selMaskAny = 0; + wandTolerance = 32; + + /* the mask outlives the call: every test after this one + * would silently run inside two pixels */ + MaskClear(); + selMaskAny = 0; + + /* + * Painting is clipped to the selection, as in the original: + * a pencil tip laid across the edge of a 2 px selection + * may only change those two columns. + */ + OPT_LOAD(); + MaskAlloc(); + MaskClear(); + MaskSet(0, 0, 1); + MaskSet(1, 0, 1); + selMaskAny = 1; + fgPixel = BlackPixel(dpy, 0); + PencilDot(2, 0, 2); + OPT_PIX("paint-sel", 0, 0, 0, 0, 0); + OPT_PIX("paint-sel", 1, 0, 0, 0, 0); + OPT_PIX("paint-sel", 3, 0, 120, 0, 128); + MaskClear(); + selMaskAny = 0; + fgPixel = BlackPixel(dpy, 0); + /* * The filters. The test picture has r = 40x, g = 60y, b = 128, * so a 3x3 box blur of the interior keeps the b channel at 128 @@ -4571,7 +6033,7 @@ main(int argc, char **argv) currentTool = saved; } FloodFillImage(30, 30); - if (SaveBMP(argv[2]) != 0) { + if (SaveImage(argv[2]) != 0) { printf("dtphoto demo: FAIL save\n"); return 1; } @@ -4588,7 +6050,7 @@ main(int argc, char **argv) /* a picture on the command line, as the original opens one */ if (argc > 1 && argv[1][0] != '-') { snprintf(path, sizeof path, "%s", argv[1]); - if (OpenBMP(path) == 0) { + if (OpenImage(path) == 0) { UpdateStatus(); RenderDoc(canvas); } diff --git a/programs/dtphoto/dtphoto.msg b/programs/dtphoto/dtphoto.msg index 63e8a458..ed8ed082 100644 --- a/programs/dtphoto/dtphoto.msg +++ b/programs/dtphoto/dtphoto.msg @@ -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 diff --git a/programs/localized/de_DE.UTF-8/msg/dtphoto.msg b/programs/localized/de_DE.UTF-8/msg/dtphoto.msg index 4e6342b0..929cfcbb 100644 --- a/programs/localized/de_DE.UTF-8/msg/dtphoto.msg +++ b/programs/localized/de_DE.UTF-8/msg/dtphoto.msg @@ -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