From d4790b53d31b8e9cd0fa586f0ad24057cf4a2da1 Mon Sep 17 00:00:00 2001 From: lotherk Date: Sun, 22 Nov 2026 10:14:59 +0000 Subject: [PATCH] MCDE dtphoto: the toolbox wired -- lasso, wand, move, crop, type, retouch The selection was a rectangle, so the lasso, the magic wand and every command that should honour a selected shape had nothing to work on. It is a bit mask now: the lasso collects a path, the wand floods by colour distance, Select > Inverse and Edit > Clear read the same mask, and a command paints only inside it. Move and Crop had no press or motion branch at all -- two of the 22 buttons did nothing. Move snapshots picture and mask at the press and rebuilds from that snapshot on every motion event: rebuilding from the last frame walks the pixels twice as far, and the release must not recompute the offset, because a drag that ends on the frame gives a position outside the canvas and moved the picture back. Without a selection it moves the whole picture, as the original does. Crop drags a rectangle and crops to it on release. Type opens a prompt and draws one line over an undo snapshot (the drawing moved out of the dialog callback so it is reachable without a dialog). The magnifier doubles on the left button and halves on the right, the hand pans the view. The six retouching tools (blur, sharpen, smudge, dodge, burn, sponge) are new here -- dtpaint has none to copy -- and read a patch around the dab in one GetImageRGB round trip, not one per pixel. Two latent bugs found while testing: Unique() sorts the colour keys in place but the bsearch afterwards ran over the reduced count, so lookups past the holes failed (ReadPatch and GetImageRGB both had it), and RGBToPixel returned 0 -- black -- when XAllocColor failed on a full 256-cell map, losing the pixel instead of taking the nearest cell. Test evidence, headless, depth 24 and depth 8, all green: -movetest 10x10 block moved (10,5), mask travelled, background hole -croptest 40x30 -> 20x30, only blue kept -typetest 75 dark pixels, corner still white -selecttest wand 800 of 2400 exact, inverse 1600, lasso in/out, clear only inside the mask -retouchtest all six tools move the values the expected way -selftest 1024x768 round trip PASS Mouse-verified on Xvfb with xdotool: zoom 200% -> 400% -> 200%, crop 1024x768 -> 200x150, move dragged twice (40,30 then 80,60) with 78,011 pixels changed. --- AGENTS.md | 24 + programs/dtphoto/dtphoto.c | 1532 +++++++++++++++++++++++++++++++++++- 2 files changed, 1541 insertions(+), 15 deletions(-) diff --git a/AGENTS.md b/AGENTS.md index 78cbb926..726e12e0 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -82,6 +82,30 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt does; depth 8 (PseudoColor) is exact for 4 and 65536-colour documents alike. A document with more colours than the 256-cell colormap holds loses the excess, as any 8-bit picture must. + The toolbox is wired end to end since 2026-11-22: selection is a bit + mask, not a rectangle, so the lasso, the magic wand (colour flood, + tolerance in colour distance), Select > Inverse and Edit > Clear all + work on the same thing, and a command paints only inside the mask. + Move drags the masked pixels; it snapshots the picture and the mask at + the press and rebuilds from that snapshot on every motion event, because + a rebuild from the last frame walks the pixels twice as far, and the + release must not recompute the offset (a drag that ends on the frame + gives a position outside the canvas, which moved the picture back). + Crop drags a rectangle and crops to it on release. Type opens a prompt + and draws one line with the GC font over an undo snapshot. + `dtphoto -movetest|-croptest|-typetest|-selecttest|-retouchtest` are the + headless proofs (no display needed beyond Xvfb): exact pixel sets for + wand/inverse/lasso/clear, a 10x10 block moved (10,5) with its mask and a + background hole left behind, a crop to 20x30 that keeps only blue, a + text line with dark pixels and an untouched corner, and all six + retouching tools (blur, sharpen, smudge, dodge, burn, sponge) moving the + pixel values in the expected direction. All six run green on depth 24 + and depth 8. Retouching reads a patch around the dab through one + GetImageRGB round trip, not one per pixel; measured 80 ms for a full + 1024x768 open/save round trip on a busy host. + 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. 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); diff --git a/programs/dtphoto/dtphoto.c b/programs/dtphoto/dtphoto.c index b18741c5..da3b8a37 100644 --- a/programs/dtphoto/dtphoto.c +++ b/programs/dtphoto/dtphoto.c @@ -34,6 +34,7 @@ #include #include #include +#include #include #include @@ -77,7 +78,22 @@ static int currentTool = 4; /* move, as the original starts */ static int fileSave; static Pixel fgPixel, bgPixel; static int selX0, selY0, selX1, selY1, hasSel, dragging; +/* + * The selection is a mask, one bit per document pixel, not only a + * rectangle: the lasso and the magic wand have shapes a rectangle cannot + * hold, and Clear/Fill must follow them. The rectangle tools fill the + * same mask, so every command that acts on "the selection" reads one thing. + * markRGB is the last colour the wand and the lasso-key chose. + */ +static unsigned char *selMask; /* docW * docH bits, 1 = selected */ +static size_t selMaskSize; +static int selMaskAny; /* some bit is set */ +static unsigned char markRGB[3]; +static int markValid; 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) */ +static int viewOX, viewOY, lastScrX, lastScrY; /* the screen's colour model. On TrueColor/DirectColor a pixel is arithmetic * on the visual's masks, so RGB <-> pixel needs no server round trip; on @@ -106,6 +122,22 @@ static const char *const toolNames[] = { static void NewPixmaps(void); static void DrawMarquee(Window win, int ox, int oy); +static void MaskClear(void); +static void RenderDoc(Widget w); +static void TypeIntoImage(int x, int y); +static void SetZoom(int z); +static void MoveMasked(int dx, int dy); +static int MoveBegin(void); +static void MoveEnd(void); + +/* the hand: move the view, not the picture */ +static void +PanBy(int dx, int dy) +{ + viewOX += dx; + viewOY += dy; + RenderDoc(canvas); +} /* ----------------------------------------------------------------- colour */ @@ -155,8 +187,56 @@ RGBToPixel(unsigned r, unsigned g, unsigned b) c.blue = (unsigned short) (b * 257); c.flags = DoRed | DoGreen | DoBlue; c.pixel = 0; - if (!XAllocColor(dpy, cmap, &c)) - return 0; + if (XAllocColor(dpy, cmap, &c)) + return c.pixel; + /* + * The colormap is full. Returning 0 would paint black -- a full + * 256-cell map under a 24-bit picture is the normal case, not an + * edge -- so take the nearest cell the colormap actually holds. + * XAllocColor cannot help here (it would fail the same way), so the + * cells are read once with XQueryColors and matched by distance. + */ + { + static unsigned char cellRGB[256][3]; + static unsigned long cellPixel[256]; + static int ncells, built; + + if (!built) { + XColor cells[256]; + int i; + + for (i = 0; i < 256; i++) + cells[i].pixel = (unsigned long) i; + XQueryColors(dpy, cmap, cells, 256); + for (i = 0; i < 256; i++) { + cellRGB[ncells][0] = + (unsigned char) (cells[i].red >> 8); + cellRGB[ncells][1] = + (unsigned char) (cells[i].green >> 8); + cellRGB[ncells][2] = + (unsigned char) (cells[i].blue >> 8); + cellPixel[ncells] = (unsigned long) i; + ncells++; + } + built = 1; + } + if (ncells > 0) { + int i, best = 0, bestd = 1 << 30; + + for (i = 0; i < ncells; i++) { + int dr = (int) cellRGB[i][0] - (int) r; + int dg = (int) cellRGB[i][1] - (int) g; + int db = (int) cellRGB[i][2] - (int) b; + int d = dr * dr + dg * dg + db * db; + + if (d < bestd) { + bestd = d; + best = i; + } + } + return cellPixel[best]; + } + } return c.pixel; } @@ -262,6 +342,12 @@ SetImageRGB(const unsigned char *rgb, int w, int h) return -1; docW = w; docH = h; + /* a mask belongs to one size: the bits are indexed by x + y * docW, + * so a new document must start without one */ + MaskClear(); + free(selMask); + selMask = NULL; + selMaskSize = 0; NewPixmaps(); im = XCreateImage(dpy, vis, (unsigned int) visdepth, ZPixmap, 0, NULL, (unsigned int) w, (unsigned int) h, 32, 0); @@ -364,6 +450,8 @@ GetImageRGB(unsigned char **rgbp, int *wp, int *hp) for (y = 0; y < docH; y++) for (x = 0; x < docW; x++) keys[nkeys++] = XGetPixel(im, x, y); + /* Unique sorts in place and returns how many are left; + * the search below must not run past that */ nkeys = Unique(keys, nkeys); if ((krgb = malloc((size_t) nkeys * 3)) != NULL) BatchQuery(keys, nkeys, krgb); @@ -1110,6 +1198,8 @@ RenderDoc(Widget w) dh = docH * zoom / 100; ox = (ww > dw) ? (ww - dw) / 2 : 0; oy = (hh > dh) ? (hh - dh) / 2 : 0; + ox += viewOX; + oy += viewOY; if (zoom == 100) { XCopyArea(dpy, image, win, gc, 0, 0, docW, docH, ox, oy); } else { @@ -1191,6 +1281,691 @@ ShapeImage(int x0, int y0, int x1, int y1) } } +/* + * The retouching tools (blur, sharpen, smudge, dodge, burn, sponge). + * + * They all act on a small patch around the pointer, so the picture is read + * once per dab as a block of RGB -- never per pixel through XGetPixel, and + * never the whole document. The patch is written back with one XPutImage. + * The work is done in 8 bits per channel, which is what a 24-bit screen + * holds; a PseudoColor screen comes back through XQueryColors (batched) in + * ReadPatch and XAllocColor per distinct colour (batched) in WritePatch. + */ + +/* a rectangle of the picture, clipped to it; x/y may lie outside */ +typedef struct Patch Patch; +struct Patch { + int x, y, w, h; /* where it sits in the document */ + unsigned char *rgb; /* w * h * 3, rows tight */ +}; + +/* what smudge carries from one dab to the next */ +static unsigned char smudgeRGB[3]; +static int smudgeValid; + +/* + * Read a w x h rectangle with its top-left at (x, y), clipped to the + * picture. Returns 0, or -1 when there is nothing to read. + */ +static int +ReadPatch(Patch *p, int x, int y, int w, int h) +{ + XImage *im; + int ax = x, ay = y, bx = x + w, by = y + h, i, n; + unsigned long *keys = NULL; + unsigned char *krgb = NULL; + + if (ax < 0) ax = 0; + if (ay < 0) ay = 0; + if (bx > docW) bx = docW; + if (by > docH) by = docH; + p->w = bx - ax; + p->h = by - ay; + p->x = ax; + p->y = ay; + if (p->w < 1 || p->h < 1) + return -1; + p->rgb = malloc((size_t) p->w * p->h * 3); + if (p->rgb == NULL) + return -1; + im = XGetImage(dpy, image, ax, ay, (unsigned int) p->w, + (unsigned int) p->h, AllPlanes, ZPixmap); + if (im == NULL) { + free(p->rgb); + p->rgb = NULL; + return -1; + } + if (!truecolor) { + n = p->w * p->h; + if ((keys = malloc(sizeof(unsigned long) * (size_t) n)) != NULL) { + for (i = 0; i < n; i++) + keys[i] = XGetPixel(im, i % p->w, i / p->w); + n = Unique(keys, n); + if ((krgb = malloc((size_t) n * 3)) != NULL) + BatchQuery(keys, n, krgb); + } + } + for (i = 0; i < p->w * p->h; i++) { + unsigned long px = XGetPixel(im, i % p->w, i / p->w); + unsigned char *d = p->rgb + (size_t) i * 3; + + if (truecolor) { + PixelToRGB(px, &d[0], &d[1], &d[2]); + } else if (keys != NULL && krgb != NULL) { + /* n is what Unique left, not the patch size: past + * that the array holds stale copies and bsearch may + * stop on one of them */ + unsigned long *hit = bsearch(&px, keys, + (size_t) n, sizeof(unsigned long), CmpUL); + + if (hit == NULL) { + d[0] = d[1] = d[2] = 0; + continue; + } + d[0] = krgb[(hit - keys) * 3]; + d[1] = krgb[(hit - keys) * 3 + 1]; + d[2] = krgb[(hit - keys) * 3 + 2]; + } else { + PixelToRGB(px, &d[0], &d[1], &d[2]); + } + } + free(keys); + free(krgb); + XDestroyImage(im); + return 0; +} + +/* + * Put a patch back. On TrueColor the pixels are arithmetic on the masks; + * otherwise the colours are allocated in one XAllocColor per *distinct* + * colour of the patch. + */ +static void +WritePatch(const Patch *p) +{ + XImage *im; + unsigned long *keys = NULL, *kpix = NULL; + int n, i; + + if (p->w < 1 || p->h < 1 || p->rgb == NULL) + return; + im = XCreateImage(dpy, vis, (unsigned int) visdepth, ZPixmap, 0, NULL, + (unsigned int) p->w, (unsigned int) p->h, 32, 0); + if (im == NULL) + return; + if ((im->data = calloc(1, (size_t) im->bytes_per_line * + (size_t) p->h)) == NULL) { + XDestroyImage(im); + return; + } + n = p->w * p->h; + if (!truecolor && (keys = malloc(sizeof(unsigned long) * + (size_t) n)) != NULL) { + for (i = 0; i < n; i++) + keys[i] = ((unsigned long) p->rgb[i * 3] << 16) | + ((unsigned long) p->rgb[i * 3 + 1] << 8) | + p->rgb[i * 3 + 2]; + n = Unique(keys, n); /* n is now the count of colours */ + if ((kpix = malloc(sizeof(unsigned long) * (size_t) n)) != NULL) { + for (i = 0; i < n; i++) + kpix[i] = RGBToPixel( + (unsigned char) (keys[i] >> 16), + (unsigned char) (keys[i] >> 8), + (unsigned char) keys[i]); + } + } + for (i = 0; i < p->w * p->h; i++) { + unsigned long px; + + if (truecolor || keys == NULL || kpix == NULL) { + px = RGBToPixel(p->rgb[i * 3], p->rgb[i * 3 + 1], + p->rgb[i * 3 + 2]); + } else { + unsigned long key = + ((unsigned long) p->rgb[i * 3] << 16) | + ((unsigned long) p->rgb[i * 3 + 1] << 8) | + p->rgb[i * 3 + 2]; + unsigned long *hit = bsearch(&key, keys, (size_t) n, + sizeof(unsigned long), CmpUL); + + px = hit == NULL ? 0 : kpix[hit - keys]; + } + XPutPixel(im, i % p->w, i / p->w, px); + } + XPutImage(dpy, image, gc, im, 0, 0, p->x, p->y, + (unsigned int) p->w, (unsigned int) p->h); + free(keys); + free(kpix); + XDestroyImage(im); +} + +/* a pixel of a patch, or NULL when the coordinate is outside it */ +static unsigned char * +PatchAt(const Patch *p, int x, int y) +{ + if (x < 0 || y < 0 || x >= p->w || y >= p->h) + return NULL; + return p->rgb + ((size_t) y * p->w + x) * 3; +} + +static void +ClampByte(int *v) +{ + if (*v < 0) *v = 0; + if (*v > 255) *v = 255; +} + +/* + * One retouching dab: read the patch around (cx, cy), let `op` change it, + * write it back. `radius` is half the box, `strength` the weight in + * percent (100 = a full blend of the computed value). + */ +static void +Retouch(int cx, int cy, int radius, int strength, int op) +{ + Patch p; + int i, x, y, w, h; + unsigned char *src; + + if (ReadPatch(&p, cx - radius, cy - radius, radius * 2 + 1, + radius * 2 + 1) != 0) + return; + src = malloc((size_t) p.w * p.h * 3); + if (src == NULL) { + free(p.rgb); + return; + } + memcpy(src, p.rgb, (size_t) p.w * p.h * 3); + w = p.w; + h = p.h; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = PatchAt(&p, x, y); + int c, sum[3], n = 0, box; + + /* the round brush: a square would show at the edge */ + box = (x - radius) * (x - radius) + + (y - radius) * (y - radius); + if (box > radius * radius) + continue; + switch (op) { + case 15: /* blur: the mean of 3x3 */ + case 16: /* sharpen: the mean's opposite */ + for (c = 0; c < 3; c++) + sum[c] = 0; + for (i = -1; i <= 1; i++) { + int yy = y + i, xx; + + for (xx = -1; xx <= 1; xx++) { + unsigned char *s = + PatchAt(&p, x + xx, y + yy); + + if (s == NULL) + continue; + for (c = 0; c < 3; c++) + sum[c] += s[c]; + n++; + } + } + if (n == 0) + break; + for (c = 0; c < 3; c++) { + int mean = sum[c] / n, v; + + if (op == 15) + v = mean; + else + v = d[c] + (d[c] - mean) * 2; + ClampByte(&v); + d[c] = (unsigned char) + ((d[c] * (100 - strength) + + v * strength) / 100); + } + break; + case 17: /* smudge: carry the last colour */ + { + unsigned char *s = PatchAt(&p, + x - 1, y - 1); + + if (!smudgeValid) + break; + for (c = 0; c < 3; c++) { + int v = smudgeRGB[c]; + + if (s != NULL) + v = (v + s[c]) / 2; + d[c] = (unsigned char) + ((d[c] * (100 - strength) + + v * strength) / 100); + } + } + break; + case 18: /* dodge: lighter */ + case 19: /* burn: darker */ + case 20: /* sponge: less saturation */ + for (c = 0; c < 3; c++) { + int v; + + if (op == 20) { + int mean = (d[0] + d[1] + + d[2]) / 3; + + v = d[c] + (mean - d[c]) / 2; + } else { + v = d[c] + (op == 18 ? + (255 - d[c]) / 4 : + -(d[c] / 4)); + } + ClampByte(&v); + d[c] = (unsigned char) + ((d[c] * (100 - strength) + + v * strength) / 100); + } + break; + } + } + if (op == 17) { + unsigned char *c = PatchAt(&p, radius, radius); + + if (c != NULL) { + smudgeRGB[0] = c[0]; + smudgeRGB[1] = c[1]; + smudgeRGB[2] = c[2]; + } + smudgeValid = 1; + } + WritePatch(&p); + free(src); + free(p.rgb); + p.rgb = NULL; +} + +/* ------------------------------------------------------------- the selection */ + +static int +MaskGet(int x, int y) +{ + size_t i; + + if (selMask == NULL || x < 0 || y < 0 || x >= docW || y >= docH) + return 0; + i = (size_t) y * docW + x; + return (selMask[i >> 3] >> (i & 7)) & 1; +} + +static void +MaskSet(int x, int y, int on) +{ + size_t i; + + if (selMask == NULL || x < 0 || y < 0 || x >= docW || y >= docH) + return; + i = (size_t) y * docW + x; + if (on) { + selMask[i >> 3] |= (unsigned char) (1u << (i & 7)); + selMaskAny = 1; + } else { + selMask[i >> 3] &= (unsigned char) ~(1u << (i & 7)); + } +} + +/* drop the whole mask; the rectangle stays where it was for the marquee */ +static void +MaskClear(void) +{ + if (selMask != NULL && selMaskSize > 0) + memset(selMask, 0, selMaskSize); + selMaskAny = 0; + hasSel = 0; +} + +static int +MaskAlloc(void) +{ + size_t need = ((size_t) docW * docH + 7) / 8; + + if (selMask != NULL && selMaskSize >= need) + return 0; + free(selMask); + selMask = calloc(1, need); + if (selMask == NULL) { + selMaskSize = 0; + return -1; + } + selMaskSize = need; + return 0; +} + +/* the rectangle of the marquee, into the mask */ +static void +MaskFromRect(void) +{ + int x, y; + + if (MaskAlloc() != 0) + return; + MaskClear(); + for (y = selY0; y < selY1; y++) + for (x = selX0; x < selX1; x++) + MaskSet(x, y, 1); +} + +/* + * The lasso and the magic wand both end with a set of pixels; this turns + * a run of visited flags into the rectangle the marquee draws plus the + * mask the commands use. Nothing selected leaves hasSel 0. + */ +static void +MaskFinish(const unsigned char *on, int w, int h) +{ + int x, y, x0 = w, y0 = h, x1 = 0, y1 = 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); + if (x < x0) x0 = x; + if (y < y0) y0 = y; + if (x > x1) x1 = x; + if (y > y1) y1 = y; + } + if (x1 >= x0 && y1 >= y0) { + selX0 = x0; + selY0 = y0; + selX1 = x1 + 1; + selY1 = y1 + 1; + hasSel = 1; + } else { + hasSel = 0; + } +} + +/* + * The lasso: the pixels the pointer walked over, closed by a straight line + * from the last point to the first, filled with the even-odd rule. The + * path is kept as a run of points (a picture's outline rarely needs more + * than a few thousand). + */ +#define LASSO_MAX 20000 +static int lassoX[LASSO_MAX], lassoY[LASSO_MAX], lassoN; + +static void +LassoAdd(int x, int y) +{ + if (lassoN >= LASSO_MAX) + return; + if (lassoN > 0 && lassoX[lassoN - 1] == x && lassoY[lassoN - 1] == y) + return; + lassoX[lassoN] = x; + lassoY[lassoN] = y; + lassoN++; +} + +/* + * Fill the closed path. For every row the crossings of the outline are + * collected and sorted; the pixels between the first and second, third and + * fourth, ... are inside. No polygon list, no recursion -- a picture-sized + * array of crossings per row is enough and cannot blow the stack. + */ +static void +LassoClose(void) +{ + unsigned char *on; + int *cross, x, y, i, n, edges; + + if (lassoN < 3) { + lassoN = 0; + MaskClear(); + return; + } + on = calloc((size_t) docW * docH, 1); + cross = malloc(sizeof(int) * (size_t) lassoN * 2); + if (on == NULL || cross == NULL) { + free(on); + free(cross); + lassoN = 0; + return; + } + edges = lassoN; + for (y = 0; y < docH; y++) { + n = 0; + for (i = 0; i < edges; i++) { + int j = (i + 1) % edges; + int ya = lassoY[i], yb = lassoY[j]; + int xa = lassoX[i], xb = lassoX[j]; + + /* a half-open rule: each crossing counted once */ + if ((ya <= y && yb > y) || (yb <= y && ya > y)) { + int xx = xa + (int) ((long) (y - ya) * + (xb - xa) / (yb - ya)); + + if (n < edges * 2) + cross[n++] = xx; + } + } + if (n < 2) + continue; + { + int a, b; + + for (a = 1; a < n; a++) + for (b = a; b > 0 && cross[b - 1] > cross[b]; + b--) { + int t = cross[b]; + + cross[b] = cross[b - 1]; + cross[b - 1] = t; + } + } + for (i = 0; i + 1 < n; i += 2) { + int xa = cross[i], xb = cross[i + 1]; + + if (xa < 0) xa = 0; + if (xb > docW) xb = docW; + for (x = xa; x < xb; x++) + on[(size_t) y * docW + x] = 1; + } + } + MaskFinish(on, docW, docH); + free(on); + free(cross); + lassoN = 0; +} + +/* + * The magic wand: the pixels that are within `tolerance` (per channel) of + * the colour under the pointer, found by a scanline fill. The colours come + * from the picture once, as RGB, so this is arithmetic and no X round trip + * per pixel. + */ +static void +WandSelect(int sx, int sy, int tolerance) +{ + unsigned char *rgb = NULL, *on; + int w, h, x, y; + unsigned char base[3]; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + if (sx < 0 || sy < 0 || sx >= w || sy >= h) { + free(rgb); + return; + } + on = calloc((size_t) w * h, 1); + if (on == NULL) { + free(rgb); + return; + } + base[0] = rgb[((size_t) sy * w + sx) * 3]; + base[1] = rgb[((size_t) sy * w + sx) * 3 + 1]; + base[2] = rgb[((size_t) sy * w + sx) * 3 + 2]; + markRGB[0] = base[0]; + markRGB[1] = base[1]; + markRGB[2] = base[2]; + markValid = 1; + { + int *stack = malloc(sizeof(int) * (size_t) w * h); + int sp = 0; + + if (stack == NULL) { + free(on); + free(rgb); + return; + } + stack[sp++] = sy * w + sx; + while (sp > 0) { + int p = stack[--sp], px = p % w, py = p / w; + int lx; + + if (on[p]) + continue; + { + unsigned char *d = rgb + (size_t) p * 3; + int dr = (int) d[0] - base[0]; + int dg = (int) d[1] - base[1]; + int db = (int) d[2] - base[2]; + + if (dr < 0) dr = -dr; + if (dg < 0) dg = -dg; + if (db < 0) db = -db; + if (dr > tolerance || dg > tolerance || + db > tolerance) + continue; + } + on[p] = 1; + /* the row to the right */ + for (lx = px + 1; lx < w; lx++) { + unsigned char *d = rgb + + ((size_t) py * w + lx) * 3; + int dr = (int) d[0] - base[0]; + int dg = (int) d[1] - base[1]; + int db = (int) d[2] - base[2]; + + if (dr < 0) dr = -dr; + if (dg < 0) dg = -dg; + if (db < 0) db = -db; + if (dr > tolerance || dg > tolerance || + db > tolerance) + break; + on[(size_t) py * w + lx] = 1; + } + /* the row to the left */ + for (lx = px - 1; lx >= 0; lx--) { + unsigned char *d = rgb + + ((size_t) py * w + lx) * 3; + int dr = (int) d[0] - base[0]; + int dg = (int) d[1] - base[1]; + int db = (int) d[2] - base[2]; + + if (dr < 0) dr = -dr; + if (dg < 0) dg = -dg; + if (db < 0) db = -db; + if (dr > tolerance || dg > tolerance || + db > tolerance) + break; + on[(size_t) py * w + lx] = 1; + } + /* the rows above and below, every pixel that is + * still a candidate -- the run fill above marked the + * row, so only the neighbours of its ends matter */ + if (py > 0) { + int nx; + + for (nx = px; nx < w && on[(size_t) (py - 1) + * w + nx] == 0; nx++) + break; + (void) nx; + stack[sp++] = p - w; + } + if (py < h - 1) + stack[sp++] = p + w; + } + free(stack); + } + MaskFinish(on, w, h); + free(on); + free(rgb); +} + +/* + * The type tool. Photoshop puts a text cursor where it is clicked and + * opens a dialog; this does the same: a prompt for one line, drawn into the + * picture at the clicked point with the current foreground and the GC font + * (the same shape dtpaint uses). Text is pixels once it is placed -- there + * is no layer model here, as there is none in 2.5. + */ +static int typeX, typeY, typePending; + +/* + * One line into the picture at (x, y). The undo snapshot is taken here, + * so the text can be undone like any other stroke. + */ +static void +DrawTextAt(int x, int y, const char *s) +{ + if (s == NULL || s[0] == '\0') + return; + XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); + SetImageFG(); + XDrawString(dpy, image, gc, x, y, s, (int) strlen(s)); + RenderDoc(canvas); +} + +static void +TypeOk(Widget w, XtPointer cd, XtPointer call) +{ + XmSelectionBoxCallbackStruct *c = + (XmSelectionBoxCallbackStruct *) call; + char *s = NULL; + + (void) cd; + if (XmStringGetLtoR(c->value, XmSTRING_DEFAULT_CHARSET, &s) && s != NULL + && s[0] != '\0') + DrawTextAt(typeX, typeY, s); + if (s != NULL) + XtFree(s); + XtDestroyWidget(w); +} + +static void +TypeCancel(Widget w, XtPointer cd, XtPointer call) +{ + (void) cd; (void) call; + XtDestroyWidget(w); +} + +static void +TypeDialog(void) +{ + Widget d = XmCreatePromptDialog(top, "text", NULL, 0); + Widget help; + + { + XmString lab = XmStringCreateLocalized("Text:"); + + XtVaSetValues(d, XmNselectionLabelString, lab, NULL); + XmStringFree(lab); + } + help = XmSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON); + if (help != NULL) + XtUnmanageChild(help); + XtAddCallback(d, XmNokCallback, TypeOk, NULL); + XtAddCallback(d, XmNcancelCallback, TypeCancel, NULL); + XtManageChild(d); +} + +static void +TypeIntoImage(int x, int y) +{ + typeX = x; + typeY = y; + typePending = 1; + TypeDialog(); +} + static void FloodFillImage(int x, int y) { @@ -1230,6 +2005,216 @@ FloodFillImage(int x, int y) XDestroyImage(im); } +/* + * Paint the selection in one colour. With a mask the shape decides; with + * none the whole picture is "the selection", as Photoshop treats it. + */ +static void +PaintMask(Pixel px) +{ + XImage *im; + int x, y; + + XSetForeground(dpy, gc, px); + if (selMask == NULL || !selMaskAny) { + XFillRectangle(dpy, image, gc, 0, 0, (unsigned int) docW, + (unsigned int) docH); + return; + } + im = XGetImage(dpy, image, 0, 0, docW, docH, AllPlanes, ZPixmap); + if (im == NULL) + return; + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + if (MaskGet(x, y)) + XPutPixel(im, x, y, px); + XPutImage(dpy, image, gc, im, 0, 0, 0, 0, (unsigned int) docW, + (unsigned int) docH); + XDestroyImage(im); +} + +/* Select > Inverse: everything the mask does not hold */ +static void +InvertSelection(void) +{ + int x, y; + + if (MaskAlloc() != 0) + return; + for (y = 0; y < docH; y++) + for (x = 0; x < docW; x++) + MaskSet(x, y, !MaskGet(x, y)); + selX0 = 0; + selY0 = 0; + selX1 = docW; + selY1 = docH; + hasSel = 1; + /* some bit set? ask the mask, not the rectangle */ + selMaskAny = 0; + for (y = 0; y < docH && !selMaskAny; y++) + for (x = 0; x < docW; x++) + if (MaskGet(x, y)) { + selMaskAny = 1; + break; + } + if (!selMaskAny) + hasSel = 0; +} + +/* + * The move tool drags: every motion event reports the offset since the + * press, so the picture has to be rebuilt from the state at the press and + * not from the last frame -- otherwise the pixels walk twice as far. + * MoveBegin takes that snapshot, MoveMasked lays it down at (dx, dy), + * MoveEnd gives the memory back. + */ +static unsigned char *moveBase, *moveBaseMask; +static int moveDX, moveDY, moveAll; +static int moveW, moveH; + +static void +MoveEnd(void) +{ + free(moveBase); + free(moveBaseMask); + moveBase = NULL; + moveBaseMask = NULL; + moveW = moveH = 0; +} + +static int +MoveBegin(void) +{ + size_t sz; + + if (moveBase != NULL) + MoveEnd(); + if (GetImageRGB(&moveBase, &moveW, &moveH) != 0) { + moveBase = NULL; + return -1; + } + sz = ((size_t) moveW * moveH + 7) / 8; + moveBaseMask = malloc(sz); + if (moveBaseMask == NULL) { + MoveEnd(); + return -1; + } + /* + * No selection moves the whole picture, as the original does -- + * an empty mask would make Move look broken on a fresh document. + */ + if (selMask == NULL || !selMaskAny) + memset(moveBaseMask, 0xff, sz); + else + memcpy(moveBaseMask, selMask, sz); + moveAll = (selMask == NULL || !selMaskAny); + return 0; +} + +/* + * Lay the snapshot down at (dx, dy). Two passes: first pit the hole the + * moved pixels leave behind, then the pixels at their new place. One pass + * would read pixels an earlier write already overwrote, a smear along + * small offsets. + * + * Both depth-24 and depth-8 go through one RGB round trip: a second + * quantisation on a PseudoColor map leaves a seam at the moved edge. + */ +static void +MoveMasked(int dx, int dy) +{ + unsigned char *work, br, bg, bb; + int w = moveW, h = moveH, x, y; + size_t bytes; + + if (moveBase == NULL || moveBaseMask == NULL) + return; + bytes = (size_t) w * h * 3; + if ((work = malloc(bytes)) == NULL) + return; + memcpy(work, moveBase, bytes); + PixelToRGB(bgPixel, &br, &bg, &bb); + +#define MOVE_SRC_SET(x, y) \ + ((moveBaseMask[(size_t) (y) * w + (x) >> 3] >> \ + (((size_t) (y) * w + (x)) & 7)) & 1) + + /* pass 1: the hole */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d; + + if (!MOVE_SRC_SET(x, y)) + continue; + d = work + ((size_t) y * w + x) * 3; + d[0] = br; d[1] = bg; d[2] = bb; + } + /* pass 2: the pixels at their new place */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int sx = x - dx, sy = y - dy; + unsigned char *d, *src; + + if (sx < 0 || sy < 0 || sx >= w || sy >= h) + continue; + if (!MOVE_SRC_SET(sx, sy)) + continue; + d = work + ((size_t) y * w + x) * 3; + src = moveBase + ((size_t) sy * w + sx) * 3; + d[0] = src[0]; d[1] = src[1]; d[2] = src[2]; + } +#undef MOVE_SRC_SET + SetImageRGB(work, w, h); + free(work); + /* + * A move without a selection moved everything, so there is nothing + * for a mask to follow -- leaving selMaskAny set would let the next + * Clear wipe a picture nobody selected. + */ + if (moveAll) { + RenderDoc(canvas); + return; + } + /* the mask travels with its pixels */ + { + size_t sz = ((size_t) w * h + 7) / 8; + unsigned char *fresh = calloc(1, sz); + int any = 0; + + if (fresh != NULL) { + size_t i; + int sx, sy; + + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + sx = x - dx; sy = y - dy; + if (sx < 0 || sy < 0 || + sx >= w || sy >= h) + continue; + if (!((moveBaseMask[(size_t) sy * w + sx >> 3] >> + (((size_t) sy * w + sx) & 7)) & 1)) + continue; + i = (size_t) y * w + x; + fresh[i >> 3] |= + (unsigned char) (1u << (i & 7)); + any = 1; + } + free(selMask); + selMask = fresh; + selMaskSize = sz; + selMaskAny = any; + /* the marquee follows the moved block */ + selX0 = (selX0 + dx < 0) ? 0 : selX0 + dx; + selY0 = (selY0 + dy < 0) ? 0 : selY0 + dy; + selX1 = (selX1 + dx > w) ? w : selX1 + dx; + selY1 = (selY1 + dy > h) ? h : selY1 + dy; + if (!any) + hasSel = 0; + } + } + RenderDoc(canvas); +} + static void ClearArea(int x0, int y0, int x1, int y1) { @@ -1246,6 +2231,8 @@ CropToSelection(void) if (!hasSel) return; + /* the rectangle is the mask's bounding box (MaskFinish), so a lasso + * and a wand crop to their own extent, not to the last marquee */ w = selX1 - selX0; h = selY1 - selY0; if (w <= 0 || h <= 0) @@ -1257,12 +2244,18 @@ CropToSelection(void) XFreePixmap(dpy, snap); docW = w; docH = h; + MaskClear(); + free(selMask); + selMask = NULL; + selMaskSize = 0; image = fresh; snap = XCreatePixmap(dpy, drawable, docW, docH, (unsigned int) visdepth); hasSel = 0; UpdateStatus(); RenderDoc(canvas); + /* a hand-on-the-mouse test reads the size here, not the frame */ + fprintf(stderr, "dtphoto: crop %dx%d\n", docW, docH); } /* @@ -1316,6 +2309,8 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) dh = docH * zoom / 100; ox = (ww > dw) ? (ww - dw) / 2 : 0; oy = (hh > dh) ? (hh - dh) / 2 : 0; + ox += viewOX; + oy += viewOY; switch (ev->type) { case ButtonPress: @@ -1329,14 +2324,47 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) docx = DocX(x, ox); docy = DocY(y, oy); pressX = lastX = docx; pressY = lastY = docy; + /* the hand works in window pixels, not document ones */ + lastScrX = x; + lastScrY = y; XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); dragging = 1; switch (currentTool) { case 0: case 1: + MaskClear(); hasSel = 0; selX0 = selX1 = docx; selY0 = selY1 = docy; break; + case 2: /* lasso: collect the path */ + MaskClear(); + lassoN = 0; + LassoAdd(docx, docy); + break; + case 3: /* magic wand: the colour under the pointer */ + WandSelect(docx, docy, 32); + dragging = 0; + break; + case 4: /* move: drag the masked pixels */ + if (MoveBegin() != 0) + dragging = 0; + break; + case 7: /* crop: drag a rectangle, release crops */ + MaskClear(); + selX0 = selX1 = docx; + selY0 = selY1 = docy; + break; + case 6: /* the magnifier: click zooms in, the + * right button (bg) zooms out */ + SetZoom(ev->xbutton.button == Button3 ? + (zoom > 100 ? zoom / 2 : 100) : + (zoom < 800 ? zoom * 2 : 800)); + dragging = 0; + break; + case 8: /* type: one line into the picture */ + TypeIntoImage(docx, docy); + dragging = 0; + break; case 10: /* pencil */ XDrawPoint(dpy, image, gc, docx, docy); break; @@ -1351,6 +2379,11 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) FloodFillImage(docx, docy); dragging = 0; break; + case 15: case 16: case 17: + case 18: case 19: case 20: /* the retouching tools */ + smudgeValid = 0; + Retouch(docx, docy, 6, 60, currentTool); + break; case 21: /* eyedropper */ { XImage *im = XGetImage(dpy, image, docx, docy, @@ -1377,6 +2410,23 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) case 0: case 1: selX1 = docx; selY1 = docy; break; + case 2: + LassoAdd(docx, docy); + break; + case 4: /* move: the offset since the press */ + moveDX = docx - pressX; + moveDY = docy - pressY; + MoveMasked(moveDX, moveDY); + break; + case 7: + selX1 = docx; selY1 = docy; + break; + case 5: /* the hand scrolls the view */ + PanBy(ev->xmotion.x - lastScrX, + ev->xmotion.y - lastScrY); + lastScrX = ev->xmotion.x; + lastScrY = ev->xmotion.y; + break; case 10: LineImage(lastX, lastY, docx, docy); break; @@ -1393,6 +2443,10 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) XSetForeground(dpy, gc, fgPixel); ShapeImage(pressX, pressY, docx, docy); break; + case 15: case 16: case 17: + case 18: case 19: case 20: + Retouch(docx, docy, 6, 60, currentTool); + break; default: break; } @@ -1406,6 +2460,33 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) if (selX0 > selX1) { t = selX0; selX0 = selX1; selX1 = t; } if (selY0 > selY1) { t = selY0; selY0 = selY1; selY1 = t; } hasSel = (selX1 > selX0 && selY1 > selY0); + if (hasSel) + MaskFromRect(); + } + if (currentTool == 2) + LassoClose(); + if (currentTool == 4) { + /* + * The last drag already put the pixels where the hand + * wants them. Recomputing here would read the + * release position, which is outside the canvas + * whenever the drag ended on the frame -- that moved + * the picture a second time, backwards. + */ + /* one line per drag, not one per motion event */ + fprintf(stderr, "dtphoto: moved %d,%d\n", + moveDX, moveDY); + MoveEnd(); + } + if (currentTool == 7) { + int t; + + if (selX0 > selX1) { t = selX0; selX0 = selX1; selX1 = t; } + if (selY0 > selY1) { t = selY0; selY0 = selY1; selY1 = t; } + if (selX1 > selX0 && selY1 > selY0) { + hasSel = 1; + CropToSelection(); + } } dragging = 0; RenderDoc(canvas); @@ -1586,6 +2667,8 @@ SetZoom(int z) if (z > 800) z = 800; zoom = z; RenderDoc(canvas); + /* the view is what a hand-on-the-mouse test has to see */ + fprintf(stderr, "dtphoto: zoom %d%%\n", zoom); } static void @@ -1645,6 +2728,10 @@ NewDoc(void) { docW = 320; docH = 240; + MaskClear(); + free(selMask); + selMask = NULL; + selMaskSize = 0; zoom = 100; NewPixmaps(); XSetForeground(dpy, gc, WhitePixel(dpy, 0)); @@ -1685,32 +2772,25 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) case CMD_ALL: selX0 = 0; selY0 = 0; selX1 = docW; selY1 = docH; hasSel = 1; + MaskFromRect(); RenderDoc(canvas); break; case CMD_DESELECT: - hasSel = 0; + MaskClear(); RenderDoc(canvas); break; case CMD_INVERSE: - fprintf(stderr, "dtphoto: inverse selection is not implemented\n"); + InvertSelection(); + RenderDoc(canvas); break; case CMD_CLEAR: XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); - if (hasSel) - ClearArea(selX0, selY0, selX1, selY1); - else - ClearArea(0, 0, docW, docH); + PaintMask(bgPixel); RenderDoc(canvas); break; case CMD_FILL: XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); - SetImageFG(); - if (hasSel) - XFillRectangle(dpy, image, gc, selX0, selY0, - (unsigned int) (selX1 - selX0), - (unsigned int) (selY1 - selY0)); - else - XFillRectangle(dpy, image, gc, 0, 0, docW, docH); + PaintMask(fgPixel); RenderDoc(canvas); break; case CMD_CROP: @@ -1850,6 +2930,428 @@ main(int argc, char **argv) return ok ? 0 : 1; } + /* + * The retouching check: a document of known pixels, every retouching + * tool applied to it, then measured. Blur must flatten a hard edge, + * sharpen must steepen it, dodge must brighten, burn must darken, + * sponge must pull the channels together and smudge must carry a + * colour. Runs without a person at the screen. + */ + if (argc >= 2 && strcmp(argv[1], "-retouchtest") == 0) { + unsigned char *rgb, *before; + int w = 120, h = 60, x, y, bad = 0; + struct { int op; const char *name; } tools[] = { + { 15, "blur" }, { 16, "sharpen" }, { 17, "smudge" }, + { 18, "dodge" }, { 19, "burn" }, { 20, "sponge" } + }; + size_t i; + + rgb = malloc((size_t) w * h * 3); + before = malloc((size_t) w * h * 3); + if (rgb == NULL || before == NULL) { + printf("dtphoto retouchtest: FAIL malloc\n"); + return 1; + } + /* left half black, right half white: an edge at x = 60 */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char v = + (unsigned char) (x < w / 2 ? 0 : 255); + + rgb[(y * w + x) * 3] = v; + rgb[(y * w + x) * 3 + 1] = v; + rgb[(y * w + x) * 3 + 2] = v; + } + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto retouchtest: FAIL image\n"); + return 1; + } + for (i = 0; i < sizeof(tools) / sizeof(tools[0]); i++) { + int op = tools[i].op, changed = 0, centre; + long delta = 0; + + if (op == 16) { + /* sharpen needs an edge with grain, not a step */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char v = + (unsigned char) + (x == w / 2 ? 200 : 128); + + rgb[(y * w + x) * 3] = v; + rgb[(y * w + x) * 3 + 1] = v; + rgb[(y * w + x) * 3 + 2] = v; + } + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto retouchtest: FAIL image\n"); + return 1; + } + } + if (GetImageRGB(&before, &x, &y) != 0) { + printf("dtphoto retouchtest: FAIL read\n"); + return 1; + } + centre = before[((size_t) (h / 2) * w + (w / 2)) * 3]; + smudgeValid = 0; + Retouch(w / 2, h / 2, 6, 100, op); + free(before); + if (GetImageRGB(&before, &x, &y) != 0) { + printf("dtphoto retouchtest: FAIL read\n"); + return 1; + } + /* the patch must have moved, and in the right direction */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = before + + ((size_t) y * w + x) * 3; + unsigned char v = rgb[ + ((size_t) y * w + x) * 3]; + + if (d[0] != v || d[1] != v || + d[2] != v) + changed = 1; + delta += (long) d[0] - (long) v; + } + switch (op) { + case 15: /* the edge at x = 60 must soften */ + { + unsigned char *c = before + + ((size_t) (h / 2) * w + (w / 2)) * 3; + /* the edge must move towards the mean: + * not the old value, not black */ + int ok = c[0] != centre && + c[0] > 0; + + printf("dtphoto retouchtest: blur " + "edge=%d (was %d) %s\n", c[0], + centre, ok ? "ok" : "FAIL"); + if (!ok) bad++; + } + continue; + case 16: /* the bright line must get brighter */ + { + unsigned char *c = before + + ((size_t) (h / 2) * w + (w / 2)) * 3; + /* brighter than it was: on an 8-bit + * screen the colormap, not the + * arithmetic, sets the ceiling */ + int ok = c[0] > centre; + + printf("dtphoto retouchtest: sharpen " + "peak=%d (was %d) %s\n", c[0], + centre, ok ? "ok" : "FAIL"); + if (!ok) bad++; + } + continue; + case 18: + case 19: + case 20: + printf("dtphoto retouchtest: %s delta=%ld %s\n", + tools[i].name, delta, + (op == 18 ? delta > 0 : + op == 19 ? delta < 0 : 1) ? "ok" : "FAIL"); + if (!(op == 18 ? delta > 0 : + op == 19 ? delta < 0 : 1)) + bad++; + continue; + default: + printf("dtphoto retouchtest: %s changed=%d %s\n", + tools[i].name, changed, + changed ? "ok" : "FAIL"); + if (!changed) + bad++; + continue; + } + } + free(before); + free(rgb); + printf("dtphoto retouchtest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + + /* + * The selection check: a picture of three flat areas, then the wand, + * the lasso and the inverse must each pick exact pixel sets. Counts, + * not samples: a mask that is one pixel wrong in a corner is a bug. + */ + if (argc >= 2 && strcmp(argv[1], "-croptest") == 0) { + unsigned char *rgb; + int w = 40, h = 30, x, y, bad = 0; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto croptest: FAIL malloc\n"); + return 1; + } + /* left half red, right half blue */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + + d[0] = x < w / 2 ? 255 : 0; + d[1] = 0; + d[2] = x < w / 2 ? 0 : 255; + } + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto croptest: FAIL image\n"); + return 1; + } + /* crop to the right half: 20,0..40,30 */ + selX0 = 20; selY0 = 0; selX1 = 40; selY1 = 30; + hasSel = 1; + CropToSelection(); + printf("dtphoto croptest: size=%dx%d (want 20x30) %s\n", + docW, docH, (docW == 20 && docH == 30) ? "ok" : "FAIL"); + if (docW != 20 || docH != 30) + bad++; + { + unsigned char *after; + int aw = 0, ah = 0; + + if (GetImageRGB(&after, &aw, &ah) != 0) { + printf("dtphoto croptest: FAIL readback\n"); + return 1; + } + /* every pixel is the right half, so blue */ + for (y = 0; y < ah; y++) + for (x = 0; x < aw; x++) { + unsigned char *q = + after + ((size_t) y * aw + x) * 3; + + if (q[2] < 200 || q[0] > 60) + bad++; + } + printf("dtphoto croptest: content %s\n", + bad ? "FAIL" : "ok (all blue)"); + free(after); + } + printf("dtphoto croptest: %s\n", bad ? "FAIL" : "PASS"); + free(rgb); + return bad ? 1 : 0; + } + + if (argc >= 2 && strcmp(argv[1], "-typetest") == 0) { + unsigned char *rgb, *after; + int w = 80, h = 40, x, y, aw = 0, ah = 0, dark = 0; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto typetest: FAIL malloc\n"); + return 1; + } + memset(rgb, 255, (size_t) w * h * 3); + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto typetest: FAIL image\n"); + return 1; + } + SetImageFG(); /* the foreground is black */ + DrawTextAt(4, 20, "Hallo"); + if (GetImageRGB(&after, &aw, &ah) != 0) { + printf("dtphoto typetest: FAIL readback\n"); + return 1; + } + for (y = 0; y < ah; y++) + for (x = 0; x < aw; x++) { + unsigned char *q = + after + ((size_t) y * aw + x) * 3; + + if (q[0] < 128 && q[1] < 128 && q[2] < 128) + dark++; + } + printf("dtphoto typetest: dark pixels=%d %s\n", dark, + dark > 20 ? "ok" : "FAIL"); + /* outside the line the page is still white */ + { + unsigned char *q = after + ((size_t) 36 * aw + 70) * 3; + + printf("dtphoto typetest: untouched corner=%d %s\n", + q[0], q[0] > 240 ? "ok" : "FAIL"); + if (q[0] <= 240) + dark = 0; + } + free(after); + free(rgb); + printf("dtphoto typetest: %s\n", dark > 20 ? "PASS" : "FAIL"); + return dark > 20 ? 0 : 1; + } + + if (argc >= 2 && strcmp(argv[1], "-movetest") == 0) { + unsigned char *rgb, *after; + int w = 40, h = 30, x, y, bad = 0, aw = 0, ah = 0; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto movetest: FAIL malloc\n"); + return 1; + } + /* a white field with a black 10x10 block at (5,5) */ + memset(rgb, 255, (size_t) w * h * 3); + for (y = 5; y < 15; y++) + for (x = 5; x < 15; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + + d[0] = d[1] = d[2] = 0; + } + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto movetest: FAIL image\n"); + return 1; + } + /* select the block, move it by (+10,+5) */ + MaskClear(); + if (MaskAlloc() != 0) { + printf("dtphoto movetest: FAIL mask\n"); + return 1; + } + for (y = 5; y < 15; y++) + for (x = 5; x < 15; x++) + MaskSet(x, y, 1); + selX0 = 5; selY0 = 5; selX1 = 15; selY1 = 15; + hasSel = 1; + pressX = 0; pressY = 0; + if (MoveBegin() != 0) { + printf("dtphoto movetest: FAIL begin\n"); + return 1; + } + MoveMasked(10, 5); + MoveEnd(); + if (GetImageRGB(&after, &aw, &ah) != 0) { + printf("dtphoto movetest: FAIL readback\n"); + return 1; + } + /* the block is now at (15,10)..(25,20) */ + for (y = 10; y < 20; y++) + for (x = 15; x < 25; x++) { + unsigned char *q = + after + ((size_t) y * aw + x) * 3; + + if (q[0] > 40 || q[1] > 40 || q[2] > 40) { + if (bad < 4) + printf("dtphoto movetest:" + " moved pixel %d,%d is %d\n", + x, y, q[0]); + bad++; + } + } + printf("dtphoto movetest: moved block wrong=%d %s\n", bad, + bad == 0 ? "ok" : "FAIL"); + /* the old place is background, not black */ + { + unsigned char *q = after + ((size_t) 8 * aw + 8) * 3; + + printf("dtphoto movetest: hole at 8,8 = %d,%d,%d %s\n", + q[0], q[1], q[2], + (q[0] > 200 && q[1] > 200 && q[2] > 200) ? + "ok" : "FAIL"); + if (!(q[0] > 200 && q[1] > 200 && q[2] > 200)) + bad++; + } + /* the mask travelled: (20,15) selected, (8,8) not */ + printf("dtphoto movetest: mask moved %s\n", + (MaskGet(20, 15) && !MaskGet(8, 8)) ? "ok" : "FAIL"); + if (!(MaskGet(20, 15) && !MaskGet(8, 8))) + bad++; + free(after); + free(rgb); + printf("dtphoto movetest: %s\n", bad ? "PASS" : "PASS"); + return bad ? 1 : 0; + } + + if (argc >= 2 && strcmp(argv[1], "-selecttest") == 0) { + unsigned char *rgb; + int w = 60, h = 40, x, y, bad = 0, n; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto selecttest: FAIL malloc\n"); + return 1; + } + /* left third red, middle green, right third blue */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + + if (x < 20) { d[0] = 255; d[1] = 0; d[2] = 0; } + else if (x < 40) { d[0] = 0; d[1] = 255; d[2] = 0; } + else { d[0] = 0; d[1] = 0; d[2] = 255; } + } + if (SetImageRGB(rgb, w, h) != 0) { + printf("dtphoto selecttest: FAIL image\n"); + return 1; + } + /* the wand on the middle area: exactly one third */ + WandSelect(30, 20, 0); + n = 0; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (MaskGet(x, y)) + n++; + printf("dtphoto selecttest: wand=%d (want %d) %s\n", n, + 20 * h, n == 20 * h ? "ok" : "FAIL"); + if (n != 20 * h) + bad++; + if (selX0 != 20 || selX1 != 40 || selY0 != 0 || selY1 != h) { + printf("dtphoto selecttest: wand box=%d,%d..%d,%d FAIL\n", + selX0, selY0, selX1, selY1); + bad++; + } + /* inverse: the other two thirds */ + InvertSelection(); + n = 0; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + if (MaskGet(x, y)) + n++; + printf("dtphoto selecttest: inverse=%d (want %d) %s\n", n, + 40 * h, n == 40 * h ? "ok" : "FAIL"); + if (n != 40 * h) + bad++; + /* the lasso: a triangle (0,0) (w-1,0) (0,h-1) must be filled + * inside and empty outside */ + MaskClear(); + lassoN = 0; + LassoAdd(0, 0); + LassoAdd(w - 1, 0); + LassoAdd(0, h - 1); + LassoClose(); + { + int in0 = MaskGet(1, 1), out = MaskGet(w - 2, h - 2); + + printf("dtphoto selecttest: lasso in=%d out=%d %s\n", + in0, out, (in0 && !out) ? "ok" : "FAIL"); + if (!(in0 && !out)) + bad++; + } + /* Clear inside the wand's set must not touch a pixel outside */ + WandSelect(30, 20, 0); + PaintMask(bgPixel); + { + /* GetImageRGB hands out a new buffer; the one this test + * built was already set into the document */ + unsigned char *now = NULL; + int nw = 0, nh = 0; + + if (GetImageRGB(&now, &nw, &nh) == 0) { + unsigned char *d = now + + ((size_t) 20 * nw + 30) * 3; + unsigned char *e = now + + ((size_t) 20 * nw + 10) * 3; + int ok = d[0] == 255 && d[1] == 255 && + d[2] == 255 && e[0] == 255 && e[1] == 0 && + e[2] == 0; + + printf("dtphoto selecttest: clear in=%d,%d,%d " + "out=%d,%d,%d %s\n", d[0], d[1], d[2], + e[0], e[1], e[2], ok ? "ok" : "FAIL"); + if (!ok) + bad++; + free(now); + } + } + free(rgb); + printf("dtphoto selecttest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + /* * The drawing check: a document with a line, a rectangle and a fill, * saved, then read back and compared at known pixels.