diff --git a/AGENTS.md b/AGENTS.md index 726e12e0..7cd1a792 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -103,6 +103,32 @@ narratives and test notes, is `docs/reference/mcde-agents-history-2026-11-13.txt 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 Image and Filter menus carry real operations since 2026-11-22, all + through the same RGB buffer: Image > Adjust (Invert, Desaturate, + Brightness/Contrast), Flip (horizontal, vertical), Rotate (180, 90 CW, + 90 CCW, which swaps the document size), Image Size (nearest neighbour, + the aspect kept from the width) and Canvas Size (the picture centred in + the larger canvas, the border in the background colour). Filter > + Blur/Sharpen are 3x3 convolutions (Blur More a gaussian-ish kernel, + Sharpen More the strong laplacian), Stylize has Find Edges, Emboss and + Solarize, Noise has Add Noise (a xorshift seed, so the same run gives + the same bytes) and Median/Despeckle, Pixelate has Mosaic. The border + pixels keep what they had -- a 3x3 window at the edge would fold + outside the picture -- and every per-pixel operation honours the + selection: the first four (invert, desaturate, brightness, solarize) + did not, which is why they now share `OpPerPixel` and a new operation + cannot forget the mask. + A menu entry is bound by its full path, not its label: the reference + has "Horizontal" in Image > Flip and in Layer > Type, and the label + alone cannot tell them apart. `CommandOf` takes "menu/sub/item", the + reader stores it in `Item.path`, and `-menutest` proves every path + resolves to the command the callback switches on (31 paths, measured + 2026-11-22). `DTPHOTO_CMDS=1` prints what the loaded menu file bound. + `dtphoto -optest|-menutest` are headless too: the operations are checked + against the arithmetic (a hard edge blurred to 170, the same edge + sharpened clamped to 255 and 0, a mosaic cell to its mean) and not + against "the picture changed". All eight run green on depth 24 and + depth 8. 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. diff --git a/programs/dtphoto/dtphoto.c b/programs/dtphoto/dtphoto.c index da3b8a37..25857c6e 100644 --- a/programs/dtphoto/dtphoto.c +++ b/programs/dtphoto/dtphoto.c @@ -46,6 +46,7 @@ typedef struct Menu Menu; typedef struct { char label[64]; + char path[128]; /* "Image/Flip/Horizontal" */ char key[16]; int separator; Menu *sub; @@ -61,7 +62,15 @@ struct Menu { 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 + CMD_ALL, CMD_DESELECT, CMD_CLEAR, CMD_FILL, CMD_CROP, CMD_INVERSE, + /* Image: the whole picture, through the RGB buffer */ + CMD_INVERT, CMD_DESATURATE, CMD_FLIPH, CMD_FLIPV, + CMD_ROT180, CMD_ROT90CW, CMD_ROT90CCW, + CMD_BRIGHTCONTRAST, CMD_IMAGESIZE, CMD_CANVASSIZE, + /* 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 }; static Menu menus[MAXMENUS]; @@ -336,19 +345,41 @@ SetImageRGB(const unsigned char *rgb, int w, int h) { XImage *im; unsigned long *keys = NULL, *kpix = NULL; - int nkeys = 0, x, y; + int nkeys = 0, x, y, same, oldW = docW, oldH = docH; if (w < 1 || h < 1 || w > 8192 || h > 8192) return -1; + same = (w == docW && h == docH); + /* + * Same size: this is a change *to* the picture, so the picture as it + * was is the undo state. Different size: a new document, nothing to + * undo. Without this an Image > Invert could not be undone -- + * SetImageRGB used to copy the *new* pixels into snap. + * + * Not while a tool drags: the canvas copies the picture into snap at + * the press, and that pre-press state is what Undo must bring back. + * Copying here mid-drag would leave the last intermediate frame in + * snap instead (a moved picture could not be undone). + */ + if (same && !dragging && image != None && snap != None) + XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); 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(); + /* + * The mask is indexed by x + y * docW, so a new size must start + * without one. A same-size change keeps it: Invert and Desaturate + * leave everything where it was, and the selection stays valid. + */ + if (!same) { + MaskClear(); + free(selMask); + selMask = NULL; + selMaskSize = 0; + } + /* the pixmaps are already the right size when nothing changed */ + if (!same || image == None) + NewPixmaps(); + (void) oldW; (void) oldH; im = XCreateImage(dpy, vis, (unsigned int) visdepth, ZPixmap, 0, NULL, (unsigned int) w, (unsigned int) h, 32, 0); if (im == NULL) @@ -963,12 +994,15 @@ Trim(char *p) p[--n] = '\0'; } +static int CommandOf(const char *path); + static void ReadMenus(const char *path) { FILE *f; char line[256]; Menu *stack[MAXDEPTH]; + char prefix[MAXDEPTH][64]; /* the path down to each level */ int depth = 0; if ((f = fopen(path, "r")) == NULL) @@ -996,6 +1030,8 @@ ReadMenus(const char *path) "%s", p + 5); menus[nmenus].count = 0; stack[0] = &menus[nmenus]; + /* "menu Layer @3.0" was already stripped of the tag */ + snprintf(prefix[0], sizeof prefix[0], "%s", p + 5); depth = 1; nmenus++; } else if (strncmp(p, "sub ", 4) == 0 && depth > 0 && @@ -1009,11 +1045,15 @@ ReadMenus(const char *path) it = &m->items[m->count++]; memset(it, 0, sizeof *it); snprintf(it->label, sizeof it->label, "%s", p + 4); + snprintf(it->path, sizeof it->path, "%s/%s", + prefix[depth - 1], p + 4); sub = calloc(1, sizeof *sub); if (sub == NULL) continue; snprintf(sub->name, sizeof sub->name, "%s", p + 4); it->sub = sub; + snprintf(prefix[depth], sizeof prefix[depth], "%s", + it->path); stack[depth++] = sub; } else if (strcmp(p, "endsub") == 0 && depth > 1) { depth--; @@ -1033,6 +1073,9 @@ ReadMenus(const char *path) } snprintf(it->label, sizeof it->label, "%s", p + 5); Trim(it->label); + snprintf(it->path, sizeof it->path, "%s/%s", + prefix[depth - 1], it->label); + it->cmd = CommandOf(it->path); } else if (strcmp(p, "sep") == 0 && depth > 0) { Menu *m = stack[depth - 1]; @@ -1044,24 +1087,58 @@ ReadMenus(const char *path) } static int -CommandOf(const char *label) +CommandOf(const char *path) { - if (strcmp(label, "New...") == 0) return CMD_NEW; - if (strcmp(label, "Open...") == 0) return CMD_OPEN; - if (strcmp(label, "Save") == 0) return CMD_SAVE; - if (strcmp(label, "Save As...") == 0) return CMD_SAVEAS; - if (strcmp(label, "Quit") == 0) return CMD_QUIT; - if (strcmp(label, "Undo") == 0) return CMD_UNDO; - if (strcmp(label, "Zoom In") == 0) return CMD_ZOOMIN; - if (strcmp(label, "Zoom Out") == 0) return CMD_ZOOMOUT; - if (strcmp(label, "Fit on Screen") == 0) return CMD_FIT; - if (strcmp(label, "Actual Pixels") == 0) return CMD_ACTUAL; - if (strcmp(label, "All") == 0) return CMD_ALL; - if (strcmp(label, "None") == 0) return CMD_DESELECT; - if (strcmp(label, "Clear") == 0) return CMD_CLEAR; - if (strcmp(label, "Fill...") == 0) return CMD_FILL; - if (strcmp(label, "Crop") == 0) return CMD_CROP; - if (strcmp(label, "Inverse") == 0) return CMD_INVERSE; + /* the label alone is ambiguous ("Horizontal" is in Image > Flip and + * in Layer > Type), so the caller passes "menu/sub/item" */ + const char *leaf = strrchr(path, '/'); + + if (leaf != NULL) + leaf++; + else + leaf = path; + if (strcmp(path, "File/New...") == 0) return CMD_NEW; + if (strcmp(path, "File/Open...") == 0) return CMD_OPEN; + if (strcmp(path, "File/Save") == 0) return CMD_SAVE; + if (strcmp(path, "File/Save As...") == 0) return CMD_SAVEAS; + if (strcmp(path, "File/Quit") == 0) return CMD_QUIT; + if (strcmp(path, "Edit/Undo") == 0) return CMD_UNDO; + if (strcmp(path, "View/Zoom In") == 0) return CMD_ZOOMIN; + 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, "Select/All") == 0) return CMD_ALL; + if (strcmp(path, "Select/None") == 0) return CMD_DESELECT; + if (strcmp(path, "Select/Inverse") == 0) return CMD_INVERSE; + 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; + /* Image: the operations that act on the whole picture */ + if (strcmp(path, "Image/Adjust/Invert") == 0) return CMD_INVERT; + if (strcmp(path, "Image/Adjust/Desaturate") == 0) return CMD_DESATURATE; + if (strcmp(path, "Image/Adjust/Brightness/Contrast...") == 0) + return CMD_BRIGHTCONTRAST; + if (strcmp(path, "Image/Flip/Horizontal") == 0) return CMD_FLIPH; + if (strcmp(path, "Image/Flip/Vertical") == 0) return CMD_FLIPV; + if (strcmp(path, "Image/Rotate/180") == 0) return CMD_ROT180; + if (strcmp(path, "Image/Rotate/90 CW") == 0) return CMD_ROT90CW; + if (strcmp(path, "Image/Rotate/90 CCW") == 0) return CMD_ROT90CCW; + /* Filter: the convolution family and the pixel filters */ + if (strcmp(path, "Filter/Blur/Blur") == 0) return CMD_BLUR; + if (strcmp(path, "Filter/Blur/Blur More") == 0) return CMD_BLURMORE; + if (strcmp(path, "Filter/Sharpen/Sharpen") == 0) return CMD_SHARPEN; + if (strcmp(path, "Filter/Sharpen/Sharpen More") == 0) + return CMD_SHARPENMORE; + if (strcmp(path, "Filter/Stylize/Find Edges") == 0) return CMD_FINDEDGES; + if (strcmp(path, "Filter/Stylize/Emboss...") == 0) return CMD_EMBOSS; + if (strcmp(path, "Filter/Stylize/Solarize") == 0) return CMD_SOLARIZE; + if (strcmp(path, "Filter/Noise/Add Noise...") == 0) return CMD_ADDNOISE; + if (strcmp(path, "Filter/Noise/Despeckle") == 0) return CMD_MEDIAN; + if (strcmp(path, "Filter/Noise/Median...") == 0) return CMD_MEDIAN; + if (strcmp(path, "Filter/Pixelate/Mosaic...") == 0) return CMD_MOSAIC; + if (strcmp(path, "Image/Image Size...") == 0) return CMD_IMAGESIZE; + if (strcmp(path, "Image/Canvas Size...") == 0) return CMD_CANVASSIZE; + (void) leaf; return CMD_NONE; } @@ -1085,6 +1162,8 @@ MakeAccel(const char *key, char *out, size_t size) snprintf(out, size, "%s%s", mod, name); } +static void DumpBound(const Menu *menus, int n); + static void MenuCallback(Widget w, XtPointer data, XtPointer call); /* dtphoto's message catalog (programs/dtphoto/dtphoto.msg, the locale's @@ -1140,7 +1219,7 @@ BuildMenu(Widget parent, Menu *m) } entry = XtVaCreateManagedWidget(it->label, xmPushButtonWidgetClass, shell, NULL); - XtAddCallback(entry, XmNactivateCallback, MenuCallback, it->label); + XtAddCallback(entry, XmNactivateCallback, MenuCallback, it); if (it->key[0] != '\0') { MakeAccel(it->key, accel, sizeof accel); XtVaSetValues(entry, XmNaccelerator, accel, NULL); @@ -1277,7 +1356,8 @@ ShapeImage(int x0, int y0, int x1, int y1) switch (currentTool) { case 9: XDrawLine(dpy, image, gc, x0, y0, x1, y1); break; case 12: XDrawRectangle(dpy, image, gc, x, y, w, h); break; - case 14: XDrawArc(dpy, image, gc, x, y, w, h, 0, 360 * 64); break; + /* case 14 is the gradient and is painted in GradientImage, not here: + * a ramp is not a shape, it fills the drag's box with a colour ramp */ } } @@ -2258,6 +2338,670 @@ CropToSelection(void) fprintf(stderr, "dtphoto: crop %dx%d\n", docW, docH); } +/* ---------------------------------------------------------- image operations */ + +/* + * Everything in the Image menu works on the whole picture and goes through + * one RGB buffer: read once, transform in memory, write once. Per pixel + * XGetPixel/XPutPixel would be a round trip each -- the defect that cost + * open and save 20 s before 2026-11-22. + * + * A transform keeps the undo snapshot: the caller passes the buffer it + * read, the new size comes back, and SetImageRGB stores image and snap. + * Where the transform has to keep the old picture (a rotation changes the + * document size), SetImageRGB takes the new one; the caller copies the old + * image into snap afterwards when the size stayed the same. + */ + +/* + * The selection clips a correction: a picture with a mask is changed only + * inside it, as in the original. Every operation goes through this, so no + * one of them can forget the mask the way the first four did. + */ +typedef void (*ChanFn)(unsigned char *p, void *cd); + +static void +OpPerPixel(ChanFn fn, void *cd) +{ + unsigned char *rgb; + int w, h, x, y; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + if (selMaskAny && !MaskGet(x, y)) + continue; + fn(rgb + ((size_t) y * w + x) * 3, cd); + } + SetImageRGB(rgb, w, h); + free(rgb); +} + +/* Invert: 255 - v on all three channels */ +static void +InvertPx(unsigned char *p, void *cd) +{ + (void) cd; + p[0] = (unsigned char) (255 - p[0]); + p[1] = (unsigned char) (255 - p[1]); + p[2] = (unsigned char) (255 - p[2]); +} + +static void +OpInvert(void) +{ + OpPerPixel(InvertPx, NULL); +} + +/* Desaturate: the luminance of the original (Photoshop uses 0.299/0.587/0.114) */ +static void +DesatPx(unsigned char *p, void *cd) +{ + (void) cd; + p[0] = p[1] = p[2] = (unsigned char) + ((299L * p[0] + 587L * p[1] + 114L * p[2]) / 1000); +} + +static void +OpDesaturate(void) +{ + OpPerPixel(DesatPx, NULL); +} + +/* Flip and rotate: the pixels move, the document keeps its size (or swaps it) */ +static int +OpFlip(int horizontal) +{ + unsigned char *rgb, *out; + int w, h, x, y; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if ((out = malloc((size_t) w * h * 3)) == NULL) { + free(rgb); + return -1; + } + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + int sx = horizontal ? w - 1 - x : x; + int sy = horizontal ? y : h - 1 - y; + + memcpy(out + ((size_t) y * w + x) * 3, + rgb + ((size_t) sy * w + sx) * 3, 3); + } + SetImageRGB(out, w, h); + free(out); + free(rgb); + return 0; +} + +/* turn: 0 = 180, 1 = 90 clockwise, -1 = 90 counter-clockwise */ +static int +OpRotate(int turn) +{ + unsigned char *rgb, *out; + int w, h, nw, nh, x, y; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if (turn == 0) { + nw = w; + nh = h; + } else { + nw = h; + nh = w; + } + if ((out = malloc((size_t) nw * nh * 3)) == NULL) { + free(rgb); + return -1; + } + for (y = 0; y < nh; y++) + for (x = 0; x < nw; x++) { + int sx, sy; + + if (turn == 0) { + sx = w - 1 - x; + sy = h - 1 - y; + } else if (turn > 0) { + /* clockwise: the left column becomes the top row */ + sx = y; + sy = h - 1 - x; + } else { + sx = w - 1 - y; + sy = x; + } + memcpy(out + ((size_t) y * nw + x) * 3, + rgb + ((size_t) sy * w + sx) * 3, 3); + } + SetImageRGB(out, nw, nh); + free(out); + free(rgb); + return 0; +} + +/* Brightness and contrast as Photoshop's own: -100..100 each */ +typedef struct { double k; double add; } BrightCD; + +static void +BrightPx(unsigned char *p, void *cd) +{ + BrightCD *bc = (BrightCD *) cd; + int i; + + for (i = 0; i < 3; i++) { + double v = (p[i] - 128.0) * bc->k + 128.0 + bc->add; + + if (v < 0.0) v = 0.0; + if (v > 255.0) v = 255.0; + p[i] = (unsigned char) (v + 0.5); + } +} + +static int +OpBrightContrast(int brightness, int contrast) +{ + BrightCD bc; + double c = (100.0 + contrast) / 100.0; + + if (c <= 0.0) + c = 0.01; + /* contrast 100 doubles, -100 flattens to nothing */ + if (contrast >= 0) + bc.k = 1.0 / (1.0 - contrast / 200.0); + else + bc.k = 1.0 + contrast / 100.0; + bc.add = brightness * 255.0 / 100.0; + OpPerPixel(BrightPx, &bc); + return 0; +} + +/* Scale the picture to (nw, nh), nearest neighbour as the original does for + * an integer zoom; enough for Image Size, which is what this is for. */ +static int +OpResize(int nw, int nh) +{ + unsigned char *rgb, *out; + int w, h, x, y; + + if (nw < 1 || nh < 1 || nw > 8192 || nh > 8192) + return -1; + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if ((out = malloc((size_t) nw * nh * 3)) == NULL) { + free(rgb); + return -1; + } + for (y = 0; y < nh; y++) + for (x = 0; x < nw; x++) { + int sx = x * w / nw; + int sy = y * h / nh; + + if (sx >= w) sx = w - 1; + if (sy >= h) sy = h - 1; + memcpy(out + ((size_t) y * nw + x) * 3, + rgb + ((size_t) sy * w + sx) * 3, 3); + } + SetImageRGB(out, nw, nh); + free(out); + free(rgb); + return 0; +} + +/* Canvas Size: keep the pixels, move them by (dx, dy) inside the new size; + * what the new canvas does not cover is background. */ +static int +OpCanvasSize(int nw, int nh, int dx, int dy) +{ + unsigned char *rgb, *out; + unsigned char br, bg, bb; + int w, h, x, y; + + if (nw < 1 || nh < 1 || nw > 8192 || nh > 8192) + return -1; + if (GetImageRGB(&rgb, &w, &h) != 0) + return -1; + if ((out = malloc((size_t) nw * nh * 3)) == NULL) { + free(rgb); + return -1; + } + PixelToRGB(bgPixel, &br, &bg, &bb); + for (y = 0; y < nh; y++) + for (x = 0; x < nw; x++) { + unsigned char *d = out + ((size_t) y * nw + x) * 3; + int sx = x - dx, sy = y - dy; + + if (sx < 0 || sy < 0 || sx >= w || sy >= h) { + d[0] = br; d[1] = bg; d[2] = bb; + } else { + memcpy(d, rgb + ((size_t) sy * w + sx) * 3, 3); + } + } + SetImageRGB(out, nw, nh); + free(out); + free(rgb); + return 0; +} + +/* + * The Filter menu: a convolution over the RGB buffer, the picture read and + * written once. A 3x3 kernel is enough for blur, sharpen, find edges and + * emboss; the border pixels keep what they had, which is what the original + * does too (it clamps instead of wrapping). + */ +static void +Convolve3x3(const double k[9], double bias) +{ + unsigned char *rgb, *src; + int w, h, x, y, c; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + if ((src = malloc((size_t) w * h * 3)) == NULL) { + free(rgb); + return; + } + memcpy(src, rgb, (size_t) w * h * 3); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + int ix, iy; + + /* the border has no neighbours to fold with */ + if (x == 0 || y == 0 || x == w - 1 || y == h - 1) { + if (selMaskAny && !MaskGet(x, y)) { + memcpy(d, src + ((size_t) y * w + x) * 3, 3); + } + continue; + } + if (selMaskAny && !MaskGet(x, y)) { + memcpy(d, src + ((size_t) y * w + x) * 3, 3); + continue; + } + for (c = 0; c < 3; c++) { + double sum = bias; + + for (iy = 0; iy < 3; iy++) + for (ix = 0; ix < 3; ix++) + sum += k[iy * 3 + ix] * + src[((size_t) (y + iy - 1) * w + + (x + ix - 1)) * 3 + c]; + if (sum < 0.0) sum = 0.0; + if (sum > 255.0) sum = 255.0; + d[c] = (unsigned char) (sum + 0.5); + } + } + free(src); + SetImageRGB(rgb, w, h); + free(rgb); +} + +static void +OpBlur(int more) +{ + /* the 3x3 box blur, the divisor 9 */ + static const double box[9] = { + 1.0 / 9, 1.0 / 9, 1.0 / 9, + 1.0 / 9, 1.0 / 9, 1.0 / 9, + 1.0 / 9, 1.0 / 9, 1.0 / 9 + }; + /* "Blur More" is a stronger, gaussian-ish kernel */ + static const double more_k[9] = { + 1.0 / 16, 2.0 / 16, 1.0 / 16, + 2.0 / 16, 4.0 / 16, 2.0 / 16, + 1.0 / 16, 2.0 / 16, 1.0 / 16 + }; + + Convolve3x3(more ? more_k : box, 0.0); +} + +static void +OpSharpen(int more) +{ + static const double sharp[9] = { + 0.0, -1.0, 0.0, + -1.0, 5.0, -1.0, + 0.0, -1.0, 0.0 + }; + static const double edge[9] = { + -1.0, -1.0, -1.0, + -1.0, 9.0, -1.0, + -1.0, -1.0, -1.0 + }; + + Convolve3x3(more ? edge : sharp, 0.0); +} + +static void +OpFindEdges(void) +{ + static const double lap[9] = { + 0.0, -1.0, 0.0, + -1.0, 4.0, -1.0, + 0.0, -1.0, 0.0 + }; + unsigned char *rgb; + int w, h, i; + + /* the edges first, then the original turns them into grey lines */ + Convolve3x3(lap, 0.0); + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + for (i = 0; i < w * h * 3; i++) + rgb[i] = (unsigned char) (255 - rgb[i]); + SetImageRGB(rgb, w, h); + free(rgb); +} + +static void +OpEmboss(void) +{ + static const double emb[9] = { + -2.0, -1.0, 0.0, + -1.0, 1.0, 1.0, + 0.0, 1.0, 2.0 + }; + + Convolve3x3(emb, 128.0); +} + +/* Noise: a deterministic scatter, so a test can predict it exactly */ +static void +OpAddNoise(int amount) +{ + unsigned char *rgb; + int w, h, x, y; + unsigned long seed = 12345UL; + + if (amount < 0) amount = 0; + if (amount > 100) amount = 100; + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *p = rgb + ((size_t) y * w + x) * 3; + int n, c; + + if (selMaskAny && !MaskGet(x, y)) + continue; + /* xorshift: no libc rand, the same value everywhere */ + seed ^= seed << 13; + seed ^= seed >> 17; + seed ^= seed << 5; + n = (int) (seed % 201UL) - 100; + n = n * amount / 100; + for (c = 0; c < 3; c++) { + int v = p[c] + n; + + if (v < 0) v = 0; + if (v > 255) v = 255; + p[c] = (unsigned char) v; + } + } + SetImageRGB(rgb, w, h); + free(rgb); +} + +/* Median: the 3x3 median per channel, the classic despeckle */ +static void +OpMedian(void) +{ + unsigned char *rgb, *src; + int w, h, x, y, c; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + if ((src = malloc((size_t) w * h * 3)) == NULL) { + free(rgb); + return; + } + memcpy(src, rgb, (size_t) w * h * 3); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *d = rgb + ((size_t) y * w + x) * 3; + + if (x == 0 || y == 0 || x == w - 1 || y == h - 1) { + memcpy(d, src + ((size_t) y * w + x) * 3, 3); + continue; + } + if (selMaskAny && !MaskGet(x, y)) { + memcpy(d, src + ((size_t) y * w + x) * 3, 3); + continue; + } + for (c = 0; c < 3; c++) { + unsigned char v[9]; + int i, j; + + for (i = 0; i < 9; i++) + v[i] = src[((size_t) (y + i / 3 - 1) * w + + (x + i % 3 - 1)) * 3 + c]; + /* nine values: an insertion sort is fastest */ + for (i = 1; i < 9; i++) { + unsigned char t = v[i]; + + for (j = i; j > 0 && v[j - 1] > t; j--) + v[j] = v[j - 1]; + v[j] = t; + } + d[c] = v[4]; + } + } + free(src); + SetImageRGB(rgb, w, h); + free(rgb); +} + +/* Pixelate: one block, one colour -- the mean of the block */ +static void +OpMosaic(int cell) +{ + unsigned char *rgb; + int w, h, x, y, bx, by, c; + + if (cell < 1) cell = 1; + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + for (by = 0; by < h; by += cell) + for (bx = 0; bx < w; bx += cell) { + int bw = bx + cell > w ? w - bx : cell; + int bh = by + cell > h ? h - by : cell; + unsigned char mean[3]; + + for (c = 0; c < 3; c++) { + unsigned long sum = 0; + int n = 0; + + for (y = by; y < by + bh; y++) + for (x = bx; x < bx + bw; x++) { + if (selMaskAny && !MaskGet(x, y)) + continue; + sum += rgb[((size_t) y * w + x) * 3 + c]; + n++; + } + mean[c] = (unsigned char) + (n > 0 ? sum / (unsigned long) n : 0); + } + for (y = by; y < by + bh; y++) + for (x = bx; x < bx + bw; x++) { + unsigned char *d; + + if (selMaskAny && !MaskGet(x, y)) + continue; + d = rgb + ((size_t) y * w + x) * 3; + memcpy(d, mean, 3); + } + } + SetImageRGB(rgb, w, h); + free(rgb); +} + +/* Solarize: the tone curve of the original, invert above mid grey */ +static void +SolarizePx(unsigned char *p, void *cd) +{ + (void) cd; + if (p[0] > 127) p[0] = (unsigned char) (255 - p[0]); + if (p[1] > 127) p[1] = (unsigned char) (255 - p[1]); + if (p[2] > 127) p[2] = (unsigned char) (255 - p[2]); +} + +static void +OpSolarize(void) +{ + OpPerPixel(SolarizePx, NULL); +} + +/* + * The gradient tool. Photoshop draws a linear ramp from the foreground to + * the background colour along the drag; the mask, when there is one, clips + * it. The picture is read once and written once, like every other op. + */ +static void +GradientImage(int x0, int y0, int x1, int y1) +{ + unsigned char *rgb, fr, fg, fb, tr, tg, tb; + int w, h, x, y; + long dx = x1 - x0, dy = y1 - y0; + long len2 = dx * dx + dy * dy; + + if (GetImageRGB(&rgb, &w, &h) != 0) + return; + if (len2 == 0) { + free(rgb); + return; + } + PixelToRGB(fgPixel, &fr, &fg, &fb); + PixelToRGB(bgPixel, &tr, &tg, &tb); + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + long t; + 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 (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); + } + SetImageRGB(rgb, w, h); + free(rgb); +} + +/* + * A one-field number dialog, the pattern the type dialog already uses. + * XmSelectionBox keeps what a person types; the callback gets the string. + */ +typedef void (*NumberFn)(int a, int b); + +static void +NumberOk(Widget w, XtPointer cd, XtPointer call) +{ + XmSelectionBoxCallbackStruct *c = + (XmSelectionBoxCallbackStruct *) call; + NumberFn fn = (NumberFn) cd; + char *s = NULL; + int a = 0, b = 0; + + if (XmStringGetLtoR(c->value, XmSTRING_DEFAULT_CHARSET, &s) && s != NULL) { + if (sscanf(s, "%d %d", &a, &b) < 1) + a = b = 0; + if (fn != NULL) + fn(a, b); + XtFree(s); + } + XtDestroyWidget(w); +} + +static void +NumberCancel(Widget w, XtPointer cd, XtPointer call) +{ + (void) cd; (void) call; + XtDestroyWidget(w); +} + +static void +NumberDialog(const char *prompt, const char *initial, NumberFn fn) +{ + Widget d = XmCreatePromptDialog(top, "number", NULL, 0); + Widget help; + XmString lab; + + lab = XmStringCreateLocalized(prompt); + XtVaSetValues(d, XmNselectionLabelString, lab, XmNtextString, + XmStringCreateLocalized(initial), NULL); + XmStringFree(lab); + help = XmSelectionBoxGetChild(d, XmDIALOG_HELP_BUTTON); + if (help != NULL) + XtUnmanageChild(help); + XtAddCallback(d, XmNokCallback, NumberOk, (XtPointer) fn); + XtAddCallback(d, XmNcancelCallback, NumberCancel, NULL); + XtManageChild(d); +} + +static void +BrightContrastOk(int brightness, int contrast) +{ + OpBrightContrast(brightness, contrast); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: brightness %d contrast %d\n", brightness, + contrast); +} + +static void +ImageSizeOk(int w, int h) +{ + (void) h; + if (w < 1) + return; + /* one field is the width; the height keeps the aspect */ + OpResize(w, docH * w / (docW > 0 ? docW : 1)); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: image size %dx%d\n", docW, docH); +} + +static void +CanvasSizeOk(int w, int h) +{ + if (w < 1 || h < 1) + return; + /* the extra canvas grows around the picture, as the original centres it */ + OpCanvasSize(w, h, (w - docW) / 2, (h - docH) / 2); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: canvas size %dx%d\n", docW, docH); +} + +static void +AddNoiseOk(int amount, int unused) +{ + (void) unused; + OpAddNoise(amount); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: add noise %d\n", amount); +} + +static void +MosaicOk(int cell, int unused) +{ + (void) unused; + OpMosaic(cell); + RenderDoc(canvas); + fprintf(stderr, "dtphoto: mosaic %d\n", cell); +} + +/* one line for a tool or a menu action, so a mouse test can read it */ +static void +OpDone(const char *what) +{ + RenderDoc(canvas); + fprintf(stderr, "dtphoto: %s\n", what); +} + /* * A dashed rectangle over the document: the marquee of the original. * Drawn on the window, not into the picture; the document keeps its pixels. @@ -2379,6 +3123,9 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) FloodFillImage(docx, docy); dragging = 0; break; + case 14: /* gradient: drag sets the direction */ + GradientImage(docx, docy, docx, docy); + break; case 15: case 16: case 17: case 18: case 19: case 20: /* the retouching tools */ smudgeValid = 0; @@ -2437,12 +3184,16 @@ CanvasInput(Widget w, XtPointer cd, XEvent *ev, Boolean *keep) XSetForeground(dpy, gc, bgPixel); Dab(docx, docy, 6); break; - case 9: case 13: case 14: + case 9: case 13: XCopyArea(dpy, snap, image, gc, 0, 0, docW, docH, 0, 0); if (currentTool == 9) XSetForeground(dpy, gc, fgPixel); ShapeImage(pressX, pressY, docx, docy); break; + case 14: /* gradient: a ramp along the drag */ + XCopyArea(dpy, snap, image, gc, 0, 0, docW, docH, 0, 0); + GradientImage(pressX, pressY, docx, docy); + break; case 15: case 16: case 17: case 18: case 19: case 20: Retouch(docx, docy, 6, 60, currentTool); @@ -2741,11 +3492,43 @@ NewDoc(void) RenderDoc(canvas); } +static void +DumpBoundMenu(const Menu *m) +{ + int j; + + for (j = 0; j < m->count; j++) { + const Item *it = &m->items[j]; + + if (it->separator) + continue; + if (it->sub != NULL) { + DumpBoundMenu(it->sub); + continue; + } + if (it->cmd == CMD_NONE) + continue; + fprintf(stderr, "dtphoto: bound %s (%d)\n", it->path, it->cmd); + } +} + +/* the list of menu entries the program can actually carry out: the reader + * fills Item.cmd, so this is what is bound, not what a document claims */ +static void +DumpBound(const Menu *menus, int n) +{ + int i; + + for (i = 0; i < n; i++) + DumpBoundMenu(&menus[i]); +} + static void MenuCallback(Widget w, XtPointer data, XtPointer call) { - const char *label = (const char *) data; - int cmd = CommandOf(label); + const Item *it = (const Item *) data; + const char *label = it != NULL ? it->label : ""; + int cmd = it != NULL ? it->cmd : CMD_NONE; (void) w; (void) call; switch (cmd) { @@ -2797,6 +3580,73 @@ MenuCallback(Widget w, XtPointer data, XtPointer call) XCopyArea(dpy, image, snap, gc, 0, 0, docW, docH, 0, 0); CropToSelection(); break; + /* Image: whole-picture operations, all through one RGB buffer */ + case CMD_INVERT: + OpInvert(); + UpdateStatus(); + OpDone("invert"); + break; + case CMD_DESATURATE: + OpDesaturate(); + UpdateStatus(); + OpDone("desaturate"); + break; + case CMD_FLIPH: + OpFlip(1); + OpDone("flip horizontal"); + break; + case CMD_FLIPV: + OpFlip(0); + OpDone("flip vertical"); + break; + case CMD_ROT180: + OpRotate(0); + UpdateStatus(); + OpDone("rotate 180"); + break; + case CMD_ROT90CW: + OpRotate(1); + UpdateStatus(); + OpDone("rotate 90 cw"); + break; + case CMD_ROT90CCW: + OpRotate(-1); + UpdateStatus(); + OpDone("rotate 90 ccw"); + break; + case CMD_BRIGHTCONTRAST: + NumberDialog("Brightness Contrast (-100..100):", "0 0", + BrightContrastOk); + break; + case CMD_IMAGESIZE: { + char buf[32]; + + snprintf(buf, sizeof buf, "%d", docW); + NumberDialog("Image Size (width):", buf, ImageSizeOk); + break; + } + case CMD_CANVASSIZE: { + char buf[32]; + + snprintf(buf, sizeof buf, "%d %d", docW, docH); + NumberDialog("Canvas Size (width height):", buf, CanvasSizeOk); + break; + } + /* Filter */ + case CMD_BLUR: OpBlur(0); OpDone("blur"); break; + case CMD_BLURMORE: OpBlur(1); OpDone("blur more"); break; + case CMD_SHARPEN: OpSharpen(0); OpDone("sharpen"); break; + case CMD_SHARPENMORE: OpSharpen(1); OpDone("sharpen more"); break; + case CMD_FINDEDGES: OpFindEdges(); OpDone("find edges"); break; + case CMD_EMBOSS: OpEmboss(); OpDone("emboss"); break; + case CMD_SOLARIZE: OpSolarize(); OpDone("solarize"); break; + case CMD_ADDNOISE: + NumberDialog("Add Noise (0..100):", "20", AddNoiseOk); + break; + case CMD_MEDIAN: OpMedian(); OpDone("median"); break; + case CMD_MOSAIC: + NumberDialog("Mosaic (cell size):", "8", MosaicOk); + break; default: fprintf(stderr, "dtphoto: %s\n", label); break; @@ -2854,6 +3704,8 @@ main(int argc, char **argv) fprintf(stderr, "dtphoto: menu[%d] \"%s\" (%d)\n", i, menus[i].name, menus[i].count); } + if (getenv("DTPHOTO_CMDS") != NULL) + DumpBound(menus, nmenus); form = XtVaCreateManagedWidget("form", xmFormWidgetClass, mainw, XmNfractionBase, 100, XmNwidth, 900, XmNheight, 600, NULL); @@ -3352,6 +4204,354 @@ main(int argc, char **argv) return bad == 0 ? 0 : 1; } + /* The menu bindings, without a mouse: every path the program claims + * to carry out must resolve to the command the callback switches + * on. A path that resolves to CMD_NONE is an entry that does + * nothing but print its own label. */ + if (argc >= 2 && strcmp(argv[1], "-menutest") == 0) { + static const struct { const char *path; int cmd; } want[] = { + { "File/New...", CMD_NEW }, + { "File/Open...", CMD_OPEN }, + { "File/Save", CMD_SAVE }, + { "File/Save As...", CMD_SAVEAS }, + { "Edit/Undo", CMD_UNDO }, + { "Edit/Clear", CMD_CLEAR }, + { "Edit/Crop", CMD_CROP }, + { "Select/All", CMD_ALL }, + { "Select/None", CMD_DESELECT }, + { "Select/Inverse", CMD_INVERSE }, + { "Image/Adjust/Invert", CMD_INVERT }, + { "Image/Adjust/Desaturate", CMD_DESATURATE }, + { "Image/Adjust/Brightness/Contrast...", CMD_BRIGHTCONTRAST }, + { "Image/Flip/Horizontal", CMD_FLIPH }, + { "Image/Flip/Vertical", CMD_FLIPV }, + { "Image/Rotate/180", CMD_ROT180 }, + { "Image/Rotate/90 CW", CMD_ROT90CW }, + { "Image/Rotate/90 CCW", CMD_ROT90CCW }, + { "Image/Image Size...", CMD_IMAGESIZE }, + { "Image/Canvas Size...", CMD_CANVASSIZE }, + { "Filter/Blur/Blur", CMD_BLUR }, + { "Filter/Blur/Blur More", CMD_BLURMORE }, + { "Filter/Sharpen/Sharpen", CMD_SHARPEN }, + { "Filter/Sharpen/Sharpen More", CMD_SHARPENMORE }, + { "Filter/Stylize/Find Edges", CMD_FINDEDGES }, + { "Filter/Stylize/Emboss...", CMD_EMBOSS }, + { "Filter/Stylize/Solarize", CMD_SOLARIZE }, + { "Filter/Noise/Add Noise...", CMD_ADDNOISE }, + { "Filter/Noise/Despeckle", CMD_MEDIAN }, + { "Filter/Noise/Median...", CMD_MEDIAN }, + { "Filter/Pixelate/Mosaic...", CMD_MOSAIC } + }; + size_t i; + int bad = 0; + + for (i = 0; i < sizeof want / sizeof want[0]; i++) { + int got = CommandOf(want[i].path); + + if (got != want[i].cmd) { + printf("dtphoto menutest: %s -> %d want %d FAIL\n", + want[i].path, got, want[i].cmd); + bad++; + } + } + /* an unknown path must not grab a command */ + if (CommandOf("Filter/Blur/No Such Thing") != CMD_NONE) { + printf("dtphoto menutest: unknown path bound FAIL\n"); + bad++; + } + printf("dtphoto menutest: %d paths, %d bad\n", + (int) (sizeof want / sizeof want[0]), bad); + printf("dtphoto menutest: %s\n", bad == 0 ? "PASS" : "FAIL"); + return bad == 0 ? 0 : 1; + } + + /* + * The image operations, checked against the arithmetic and not against + * "the picture changed": invert is 255-v, desaturate is the luminance, + * a flip is an exact permutation, a rotation moves a known corner to a + * known corner. A wrong one of these is a wrong picture, not a bug + * that shows later. + */ + if (argc >= 2 && strcmp(argv[1], "-optest") == 0) { + unsigned char *rgb; + int w = 6, h = 4, x, y, bad = 0; + + rgb = malloc((size_t) w * h * 3); + if (rgb == NULL) { + printf("dtphoto optest: FAIL malloc\n"); + return 1; + } + /* a picture where every pixel is distinct: + * r = x*40, g = y*60, b = 128 */ + 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 * 40); + p[1] = (unsigned char) (y * 60); + p[2] = 128; + } + +#define OPT_LOAD() do { \ + if (SetImageRGB(rgb, w, h) != 0) { \ + printf("dtphoto optest: FAIL image\n"); \ + free(rgb); return 1; \ + } \ +} while (0) + /* fetch the document and check one pixel against (r,g,b) */ +#define OPT_PIX(tag, px, py, er, eg, eb) do { \ + unsigned char *now = NULL; int nw = 0, nh = 0; \ + if (GetImageRGB(&now, &nw, &nh) != 0) { \ + printf("dtphoto optest: FAIL readback\n"); \ + free(rgb); return 1; \ + } \ + if (px < nw && py < nh) { \ + unsigned char *q = now + ((size_t) (py) * nw + (px)) * 3; \ + int ok = q[0] == (er) && q[1] == (eg) && q[2] == (eb); \ + printf("dtphoto optest: %s (%d,%d)=%d,%d,%d want %d,%d,%d %s\n", \ + tag, px, py, q[0], q[1], q[2], er, eg, eb, ok ? "ok" : "FAIL"); \ + if (!ok) bad++; \ + } else { \ + printf("dtphoto optest: %s (%d,%d) outside %dx%d FAIL\n", \ + tag, px, py, nw, nh); \ + bad++; \ + } \ + free(now); \ +} while (0) + + /* invert: the corner (0,0) = 0,0,128 becomes 255,255,127 */ + OPT_LOAD(); + OpInvert(); + OPT_PIX("invert", 0, 0, 255, 255, 127); + OPT_PIX("invert", 5, 3, 55, 75, 127); + + /* desaturate: (0,0,128) -> 14; (5,3)=(200,180,128) -> + * (299*200 + 587*180 + 114*128)/1000 = 180 */ + OPT_LOAD(); + OpDesaturate(); + OPT_PIX("desat", 0, 0, 14, 14, 14); + OPT_PIX("desat", 5, 3, 180, 180, 180); + + /* flip horizontal: the left column ends up on the right */ + OPT_LOAD(); + OpFlip(1); + OPT_PIX("fliph", 5, 0, 0, 0, 128); + OPT_PIX("fliph", 0, 0, 200, 0, 128); + + /* flip vertical: the top row ends up at the bottom */ + OPT_LOAD(); + OpFlip(0); + OPT_PIX("flipv", 0, 3, 0, 0, 128); + OPT_PIX("flipv", 0, 0, 0, 180, 128); + + /* rotate 180: the corner (0,0) lands on (w-1,h-1) */ + OPT_LOAD(); + OpRotate(0); + OPT_PIX("rot180", 5, 3, 0, 0, 128); + OPT_PIX("rot180", 0, 0, 200, 180, 128); + + /* rotate 90 clockwise: the document is now h wide, w high; + * the top-left pixel of the result is the bottom-left of the + * original */ + OPT_LOAD(); + OpRotate(1); + OPT_PIX("rot90cw", 0, 0, 0, 180, 128); + OPT_PIX("rot90cw", 3, 5, 200, 0, 128); + + /* rotate 90 counter-clockwise: the top-left is the top-right */ + OPT_LOAD(); + OpRotate(-1); + OPT_PIX("rot90ccw", 0, 0, 200, 0, 128); + OPT_PIX("rot90ccw", 3, 5, 0, 180, 128); + + /* brightness +100 lifts everything to white, -100 to black */ + OPT_LOAD(); + OpBrightContrast(100, 0); + OPT_PIX("bright+100", 0, 0, 255, 255, 255); + OPT_LOAD(); + OpBrightContrast(-100, 0); + OPT_PIX("bright-100", 0, 0, 0, 0, 0); + /* contrast 0 leaves the picture alone */ + OPT_LOAD(); + OpBrightContrast(0, 0); + OPT_PIX("contrast0", 3, 1, 120, 60, 128); + + /* resize to double: (2,2) is what (1,1) held -- sx=1, sy=1 */ + OPT_LOAD(); + OpResize(12, 8); + OPT_PIX("resize", 2, 2, 40, 60, 128); + OPT_PIX("resize", 11, 7, 200, 180, 128); + + /* canvas size 10x8 keeps the picture in the middle: the old + * (0,0) is now at (2,2) */ + OPT_LOAD(); + OpCanvasSize(10, 8, 2, 2); + OPT_PIX("canvas", 2, 2, 0, 0, 128); + OPT_PIX("canvas", 0, 0, 255, 255, 255); + + /* the gradient: a ramp from black to white along x */ + OPT_LOAD(); + fgPixel = BlackPixel(dpy, 0); + bgPixel = WhitePixel(dpy, 0); + GradientImage(0, 0, w - 1, 0); + OPT_PIX("gradient", 0, 0, 0, 0, 0); + OPT_PIX("gradient", 5, 0, 255, 255, 255); + OPT_PIX("gradient", 5, 3, 255, 255, 255); + + /* + * 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 + * and leaves r/g at their own value (a ramp blur is the value + * in the middle of the window). + */ + OPT_LOAD(); + OpBlur(0); + OPT_PIX("blur", 2, 1, 80, 60, 128); + /* the border keeps what it had: no neighbours to fold with */ + OPT_PIX("blur", 0, 0, 0, 0, 128); + + /* + * A ramp blur keeps the ramp, so that alone proves nothing. + * A hard edge does: a picture white on the left half, black on + * the right, blurred, must have grey where the edge was and + * white/black at the untouched border. + */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *p = rgb + ((size_t) y * w + x) * 3; + + p[0] = p[1] = p[2] = x < 3 ? 255 : 0; + } + OPT_LOAD(); + OpBlur(0); + /* (2,1): the window is x=1..3, three rows -- six white of nine: + * 1530/9 = 170 */ + OPT_PIX("blur-edge", 2, 1, 170, 170, 170); + /* the corner has no neighbours, so it stays white */ + OPT_PIX("blur-edge", 0, 0, 255, 255, 255); + /* the far side stays black */ + OPT_PIX("blur-edge", 5, 3, 0, 0, 0); + + /* and sharpen pushes the edge further apart than blur does: + * 5*200 - (200+200+200+56) = 344, clamped to 255 */ + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) { + unsigned char *p = rgb + ((size_t) y * w + x) * 3; + + p[0] = p[1] = p[2] = x < 3 ? 200 : 56; + } + OPT_LOAD(); + OpSharpen(0); + OPT_PIX("sharpen-edge", 2, 1, 255, 255, 255); + /* on the dark side: 5*56 - (200+56+56+56) = -88, so black */ + OPT_PIX("sharpen-edge", 3, 1, 0, 0, 0); + + /* the edge picture replaced the ramp; put the ramp back */ + 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 * 40); + p[1] = (unsigned char) (y * 60); + p[2] = 128; + } + + /* median on a linear ramp is the value itself */ + OPT_LOAD(); + OpMedian(); + OPT_PIX("median", 2, 1, 80, 60, 128); + OPT_PIX("median", 0, 0, 0, 0, 128); + + /* solarize: every channel above mid grey is inverted; + * (0,0,128) -> (0,0,127) and (200,180,128) -> (55,75,127) */ + OPT_LOAD(); + OpSolarize(); + OPT_PIX("solarize", 0, 0, 0, 0, 127); + OPT_PIX("solarize", 5, 3, 55, 75, 127); + + /* mosaic 2: the block (2..3, 0..1) gets the mean of its four + * pixels: r = (80+120+80+120)/4 = 100, g = (0+0+60+60)/4 = 30 */ + OPT_LOAD(); + OpMosaic(2); + OPT_PIX("mosaic", 2, 0, 100, 30, 128); + OPT_PIX("mosaic", 3, 1, 100, 30, 128); + + /* + * Noise: deterministic (one seed), so the same call twice gives + * the same bytes; and it never leaves the 0..255 range. + */ + { + unsigned char *a = NULL, *b = NULL; + int aw = 0, ah = 0, bw = 0, bh = 0, i, same = 1; + int spread = 0; + + OPT_LOAD(); + OpAddNoise(50); + GetImageRGB(&a, &aw, &ah); + OPT_LOAD(); + OpAddNoise(50); + GetImageRGB(&b, &bw, &bh); + if (a != NULL && b != NULL && aw == bw && ah == bh) { + for (i = 0; i < aw * ah * 3; i++) + if (a[i] != b[i]) + same = 0; + } else { + same = 0; + } + /* the noise must actually have moved something */ + for (i = 0; a != NULL && i < aw * ah; i += 3) { + if (a[i] != (int) (40 * ((i / 3) % aw))) + spread = 1; + } + printf("dtphoto optest: noise deterministic %s, moved %s\n", + same ? "ok" : "FAIL", spread ? "ok" : "FAIL"); + if (!same || !spread) + bad++; + free(a); + free(b); + } + + /* + * The selection clips a correction. A mask over the left half + * means Invert must leave the right half alone. + */ + { + unsigned char *now = NULL; + int nw = 0, nh = 0, inside = 0, outside = 0; + + OPT_LOAD(); + if (MaskAlloc() != 0) { + printf("dtphoto optest: FAIL mask alloc\n"); + free(rgb); + return 1; + } + selMaskAny = 1; + for (y = 0; y < h; y++) + for (x = 0; x < w; x++) + MaskSet(x, y, x < w / 2); + OpInvert(); + GetImageRGB(&now, &nw, &nh); + if (now != NULL) { + unsigned char *in = now + 0; + unsigned char *out = now + ((size_t) 1 * nw + + (nw - 1)) * 3; + + inside = in[0] == 255 && in[2] == 127; + outside = out[0] == 200 && out[2] == 128; + } + printf("dtphoto optest: mask inside %s, outside %s\n", + inside ? "ok" : "FAIL", outside ? "ok" : "FAIL"); + if (!inside || !outside) + bad++; + free(now); + MaskClear(); + } + + free(rgb); + printf("dtphoto optest: %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.