--- ray/src/px/x11image.c 1992/10/22 17:51:05 2.16 +++ ray/src/px/x11image.c 2004/01/02 12:47:01 2.65 @@ -1,9 +1,6 @@ -/* Copyright (c) 1992 Regents of the University of California */ - #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static const char RCSid[] = "$Id"; #endif - /* * x11image.c - driver for X-windows * @@ -21,15 +18,18 @@ static char SCCSid[] = "$SunId$ LBL"; #include "standard.h" +#include +#include #include #include #include +#include #include "color.h" +#include "tonemap.h" #include "view.h" #include "x11raster.h" #include "random.h" -#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ @@ -39,6 +39,8 @@ static char SCCSid[] = "$SunId$ LBL"; #define ICONSIZ (8*10) /* maximum icon dimension (even 8) */ +#define FIXWEIGHT 20 /* weight to add for fixation points */ + #define ourscreen DefaultScreen(thedisplay) #define ourroot RootWindow(thedisplay,ourscreen) @@ -47,6 +49,7 @@ static char SCCSid[] = "$SunId$ LBL"; #define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) double gamcor = 2.2; /* gamma correction */ +char *gamstr = NULL; /* gamma value override */ int dither = 1; /* dither colors? */ int fast = 0; /* keep picture in Pixmap? */ @@ -55,8 +58,6 @@ char *dispname = NULL; /* our display name */ Window wind = 0; /* our output window */ unsigned long ourblack=0, ourwhite=1; /* black and white for this visual */ -Font fontid; /* our font */ - int maxcolors = 0; /* maximum colors */ int greyscale = 0; /* in grey */ @@ -65,16 +66,27 @@ int scale = 0; /* scalefactor; power of two */ int xoff = 0; /* x image offset */ int yoff = 0; /* y image offset */ +int parent = 0; /* number of children, -1 if child */ +int sequential = 0; /* display images in sequence */ + +char *tout = "od"; /* output of 't' command */ +int tinterv = 0; /* interval between mouse reports */ + +int tmflags = TM_F_LINEAR; /* tone mapping flags */ + VIEW ourview = STDVIEW; /* image view parameters */ int gotview = 0; /* got parameters from file */ COLR *scanline; /* scan line buffer */ +TMbright *lscan; /* encoded luminance scanline */ +BYTE *cscan; /* encoded chroma scanline */ +BYTE *pscan; /* compute pixel scanline */ RESOLU inpres; /* input resolution and ordering */ int xmax, ymax; /* picture dimensions */ int width, height; /* window size */ char *fname = NULL; /* input file name */ -FILE *fin = stdin; /* input file */ +FILE *fin = NULL; /* input file */ long *scanpos = NULL; /* scan line positions in file */ int cury = 0; /* current scan location */ @@ -92,7 +104,7 @@ unsigned char *ourdata; /* our image data */ struct { int xmin, ymin, xsiz, ysiz; -} box = {0, 0, 0, 0}; /* current box */ +} bbox = {0, 0, 0, 0}; /* current bbox */ char *geometry = NULL; /* geometry specification */ @@ -103,59 +115,82 @@ char *progname; char errmsg[128]; -extern BYTE clrtab[256][3]; /* global color map */ +BYTE clrtab[256][3]; /* global color map */ extern long ftell(); -extern char *malloc(), *calloc(); +extern char *getenv(); Display *thedisplay; +Atom closedownAtom, wmProtocolsAtom; +int sigrecv; +void onsig(int i) { sigrecv++; } + +static gethfunc headline; + + main(argc, argv) int argc; char *argv[]; { - extern char *getenv(); - char *gv; - int headline(); int i; + int pid; progname = argv[0]; - if ((gv = getenv("GAMMA")) != NULL) - gamcor = atof(gv); + fin = stdin; for (i = 1; i < argc; i++) if (argv[i][0] == '-') switch (argv[i][1]) { - case 'c': + case 'c': /* number of colors */ maxcolors = atoi(argv[++i]); break; - case 'b': + case 'b': /* greyscale only */ greyscale = !greyscale; break; - case 'm': + case 'm': /* monochrome */ + greyscale = 1; maxcolors = 2; break; - case 'd': + case 'd': /* display or dither */ if (argv[i][2] == 'i') dispname = argv[++i]; else dither = !dither; break; - case 'f': + case 'f': /* save pixmap */ fast = !fast; break; - case 'e': - if (argv[i+1][0] != '+' && argv[i+1][0] != '-') + case 's': /* one at a time */ + sequential = !sequential; + break; + case 'o': /* 't' output */ + tout = argv[i]+2; + break; + case 't': /* msec interval */ + tinterv = atoi(argv[++i]); + break; + case 'e': /* exposure comp. */ + i++; + if (argv[i][0] == 'a') { + tmflags = TM_F_CAMERA; + break; + } + if (argv[i][0] == 'h') { + tmflags = TM_F_HUMAN; + break; + } + if (argv[i][0] != '+' && argv[i][0] != '-') goto userr; - scale = atoi(argv[++i]); + scale = atoi(argv[i]); break; - case 'g': + case 'g': /* gamma comp. */ if (argv[i][2] == 'e') geometry = argv[++i]; else - gamcor = atof(argv[++i]); + gamstr = argv[++i]; break; default: goto userr; @@ -165,15 +200,28 @@ char *argv[]; else break; - if (i == argc-1) { + if (i > argc) + goto userr; + while (i < argc-1) { + sigrecv = 0; + signal(SIGCONT, onsig); + if ((pid=fork()) == 0) { /* a child for each picture */ + parent = -1; + break; + } + if (pid < 0) + quiterr("fork failed"); + parent++; + while (!sigrecv) + pause(); /* wait for wake-up call */ + i++; + } + if (i < argc) { /* open picture file */ fname = argv[i]; fin = fopen(fname, "r"); - if (fin == NULL) { - sprintf(errmsg, "cannot open file \"%s\"", fname); - quiterr(errmsg); - } - } else if (i != argc) - goto userr; + if (fin == NULL) + quiterr("cannot open picture file"); + } /* get header */ getheader(fin, headline, NULL); /* get picture dimensions */ @@ -187,123 +235,171 @@ char *argv[]; if ((scanline = (COLR *)malloc(xmax*sizeof(COLR))) == NULL) quiterr("out of memory"); - init(); /* get file and open window */ + init(argc, argv); /* get file and open window */ for ( ; ; ) getevent(); /* main loop */ userr: fprintf(stderr, - "Usage: %s [-display disp][-geometry spec][-b][-m][-d][-f][-c ncolors][-e +/-stops] file\n", +"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", progname); - quit(1); + exit(1); } -headline(s) /* get relevant info from header */ -char *s; +static int +headline( /* get relevant info from header */ + char *s, + void *p +) { char fmt[32]; if (isexpos(s)) exposure *= exposval(s); - else if (isformat(s)) { - formatval(fmt, s); + else if (formatval(fmt, s)) wrongformat = strcmp(fmt, COLRFMT); - } else if (isview(s) && sscanview(&ourview, s) > 0) + else if (isview(s) && sscanview(&ourview, s) > 0) gotview++; + return(0); } -init() /* get data and open window */ +init(argc, argv) /* get data and open window */ +int argc; +char **argv; { - XWMHints ourxwmhints; XSetWindowAttributes ourwinattr; - XSizeHints oursizhints; + XClassHint xclshints; + XWMHints xwmhints; + XSizeHints xszhints; + XTextProperty windowName, iconName; + XGCValues xgcv; + char *name; register int i; if (fname != NULL) { scanpos = (long *)malloc(ymax*sizeof(long)); if (scanpos == NULL) - goto memerr; + quiterr("out of memory"); for (i = 0; i < ymax; i++) scanpos[i] = -1; - } + name = fname; + } else + name = progname; + /* remove directory prefix from name */ + for (i = strlen(name); i-- > 0; ) + if (name[i] == '/') + break; + name += i+1; if ((thedisplay = XOpenDisplay(dispname)) == NULL) quiterr("cannot open display"); + /* set gamma value */ + if (gamstr == NULL) /* get it from the X server */ + gamstr = XGetDefault(thedisplay, "radiance", "gamma"); + if (gamstr == NULL) /* get it from the environment */ + gamstr = getenv("DISPLAY_GAMMA"); + if (gamstr != NULL) + gamcor = atof(gamstr); /* get best visual for default screen */ getbestvis(); /* store image */ getras(); - /* open window */ - ourwinattr.border_pixel = ourblack; - ourwinattr.background_pixel = ourwhite; - ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, - ourvis.visual, AllocNone); - wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, - ourvis.depth, InputOutput, ourvis.visual, - CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); - if (wind == 0) - quiterr("cannot create window"); - XFreeColormap(thedisplay, ourwinattr.colormap); - width = xmax; - height = ymax; - ourgc = XCreateGC(thedisplay, wind, 0, 0); - XSetState(thedisplay, ourgc, ourblack, ourwhite, GXcopy, AllPlanes); - revgc = XCreateGC(thedisplay, wind, 0, 0); - XSetFunction(thedisplay, revgc, GXinvert); - fontid = XLoadFont(thedisplay, FONTNAME); - if (fontid == 0) - quiterr("cannot get font"); - XSetFont(thedisplay, ourgc, fontid); - XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, - XC_diamond_cross)); - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); + /* get size and position */ + xszhints.flags = 0; + xszhints.width = xmax; xszhints.height = ymax; if (geometry != NULL) { - bzero((char *)&oursizhints, sizeof(oursizhints)); - i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, - (unsigned *)&oursizhints.width, - (unsigned *)&oursizhints.height); + i = XParseGeometry(geometry, &xszhints.x, &xszhints.y, + (unsigned *)&xszhints.width, + (unsigned *)&xszhints.height); if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) - oursizhints.flags |= USSize; - else { - oursizhints.width = xmax; - oursizhints.height = ymax; - oursizhints.flags |= PSize; - } + xszhints.flags |= USSize; + else + xszhints.flags |= PSize; if ((i&(XValue|YValue)) == (XValue|YValue)) { - oursizhints.flags |= USPosition; + xszhints.flags |= USPosition; if (i & XNegative) - oursizhints.x += DisplayWidth(thedisplay, - ourscreen)-1-oursizhints.width-2*BORWIDTH; + xszhints.x += DisplayWidth(thedisplay, + ourscreen)-1-xszhints.width-2*BORWIDTH; if (i & YNegative) - oursizhints.y += DisplayHeight(thedisplay, - ourscreen)-1-oursizhints.height-2*BORWIDTH; + xszhints.y += DisplayHeight(thedisplay, + ourscreen)-1-xszhints.height-2*BORWIDTH; } - XSetNormalHints(thedisplay, wind, &oursizhints); } - ourxwmhints.flags = InputHint|IconPixmapHint; - ourxwmhints.input = True; - ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, + /* open window */ + i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; + ourwinattr.border_pixel = ourwhite; + ourwinattr.background_pixel = ourblack; + if (ourvis.visual != DefaultVisual(thedisplay,ourscreen)) { + ourwinattr.colormap = newcmap(thedisplay, ourscreen, ourvis.visual); + i |= CWColormap; + } + ourwinattr.event_mask = ExposureMask|KeyPressMask|ButtonPressMask| + ButtonReleaseMask|ButtonMotionMask|StructureNotifyMask; + ourwinattr.cursor = XCreateFontCursor(thedisplay, XC_diamond_cross); + wind = XCreateWindow(thedisplay, ourroot, xszhints.x, xszhints.y, + xszhints.width, xszhints.height, BORWIDTH, + ourvis.depth, InputOutput, ourvis.visual, + i, &ourwinattr); + if (wind == 0) + quiterr("cannot create window"); + width = xmax; + height = ymax; + /* prepare graphics drawing context */ + if ((xgcv.font = XLoadFont(thedisplay, FONTNAME)) == 0) + quiterr("cannot get font"); + xgcv.foreground = ourblack; + xgcv.background = ourwhite; + ourgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| + GCFont, &xgcv); + xgcv.function = GXinvert; + revgc = XCreateGC(thedisplay, wind, GCForeground|GCBackground| + GCFunction, &xgcv); + + /* set up the window manager */ + xwmhints.flags = InputHint|IconPixmapHint; + xwmhints.input = True; + xwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, wind, icondata, iconwidth, iconheight); - XSetWMHints(thedisplay, wind, &ourxwmhints); - XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask - |ButtonMotionMask|StructureNotifyMask - |KeyPressMask|ExposureMask); + + windowName.encoding = iconName.encoding = XA_STRING; + windowName.format = iconName.format = 8; + windowName.value = (u_char *)name; + windowName.nitems = strlen(windowName.value); + iconName.value = (u_char *)name; + iconName.nitems = strlen(windowName.value); + + xclshints.res_name = NULL; + xclshints.res_class = "Ximage"; + XSetWMProperties(thedisplay, wind, &windowName, &iconName, + argv, argc, &xszhints, &xwmhints, &xclshints); + closedownAtom = XInternAtom(thedisplay, "WM_DELETE_WINDOW", False); + wmProtocolsAtom = XInternAtom(thedisplay, "WM_PROTOCOLS", False); + XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); + XMapWindow(thedisplay, wind); - return; -memerr: - quiterr("out of memory"); } /* end of init */ quiterr(err) /* print message and exit */ char *err; { - if (err != NULL) { - fprintf(stderr, "%s: %s\n", progname, err); - exit(1); + register int es; + int cs; + + if ( (es = err != NULL) ) + fprintf(stderr, "%s: %s: %s\n", progname, + fname==NULL?"":fname, err); + if (thedisplay != NULL) + XCloseDisplay(thedisplay); + if ((parent < 0) & (sigrecv == 0)) + kill(getppid(), SIGCONT); + while (parent > 0 && wait(&cs) != -1) { /* wait for any children */ + if (es == 0) + es = cs>>8 & 0xff; + parent--; } - exit(0); + exit(es); } @@ -321,19 +417,20 @@ register XVisualInfo *v1, *v2; return(-1); if (v1->depth == 32 && v2->depth == 24) return(1); - return(0); + /* go for maximum depth otherwise */ + return(v2->depth - v1->depth); } /* don't be too greedy */ if (maxcolors <= 1<depth && maxcolors <= 1<depth) return(v1->depth - v2->depth); return(v2->depth - v1->depth); } - /* prefer Pseudo when < 24-bit */ + /* prefer Pseudo when < 15-bit */ if ((v1->class == TrueColor || v1->class == DirectColor) && - v1->depth < 24) + v1->depth < 15) bad1 = 1; if ((v2->class == TrueColor || v2->class == DirectColor) && - v2->depth < 24) + v2->depth < 15) bad2 = -1; if (bad1 | bad2) return(bad1+bad2); @@ -400,7 +497,7 @@ static char vistype[][12] = { if (viscmp(&xvi[i],&ourvis) > 0) quiterr("inadequate visuals on this screen"); /* OK, we'll use it */ - copystruct(&ourvis, &xvi[i]); + ourvis = xvi[i]; #ifdef DEBUG fprintf(stderr, "Selected visual type %s, depth %d\n", vistype[ourvis.class], ourvis.depth); @@ -443,12 +540,13 @@ getras() /* get raster file */ if (ourras == NULL) goto fail; getmono(); - } else if (ourvis.class == TrueColor | ourvis.class == DirectColor) { - ourdata = (unsigned char *)malloc(4*xmax*ymax); + } else if ((ourvis.class == TrueColor) | (ourvis.class == DirectColor)) { + int datsiz = ourvis.depth>16 ? sizeof(int32) : sizeof(int16); + ourdata = (unsigned char *)malloc(datsiz*xmax*ymax); if (ourdata == NULL) goto fail; - ourras = make_raster(thedisplay, &ourvis, 32, - ourdata, xmax, ymax, 32); + ourras = make_raster(thedisplay, &ourvis, datsiz*8, + ourdata, xmax, ymax, datsiz*8); if (ourras == NULL) goto fail; getfull(); @@ -460,7 +558,7 @@ getras() /* get raster file */ xmax, ymax, 8); if (ourras == NULL) goto fail; - if (greyscale | ourvis.class == StaticGray) + if (greyscale) getgrey(); else getmapped(); @@ -475,46 +573,103 @@ fail: getevent() /* process the next event */ { - union { - XEvent u; - XConfigureEvent c; - XExposeEvent e; - XButtonPressedEvent b; - XKeyPressedEvent k; - } e; + XEvent xev; - XNextEvent(thedisplay, &e.u); - switch (e.u.type) { + XNextEvent(thedisplay, &xev); + switch ((int)xev.type) { case KeyPress: - docom(&e.k); + docom(&xev.xkey); break; case ConfigureNotify: - width = e.c.width; - height = e.c.height; + width = xev.xconfigure.width; + height = xev.xconfigure.height; break; case MapNotify: map_rcolors(ourras, wind); if (fast) make_rpixmap(ourras, wind); + if ((!sequential) & (parent < 0) & (sigrecv == 0)) { + kill(getppid(), SIGCONT); + sigrecv--; + } break; case UnmapNotify: if (!fast) unmap_rcolors(ourras); break; case Expose: - redraw(e.e.x, e.e.y, e.e.width, e.e.height); + redraw(xev.xexpose.x, xev.xexpose.y, + xev.xexpose.width, xev.xexpose.height); break; case ButtonPress: - if (e.b.state & (ShiftMask|ControlMask)) - moveimage(&e.b); + if (xev.xbutton.state & (ShiftMask|ControlMask)) + moveimage(&xev.xbutton); else - getbox(&e.b); + switch (xev.xbutton.button) { + case Button1: + getbox(&xev.xbutton); + break; + case Button2: + traceray(xev.xbutton.x, xev.xbutton.y); + break; + case Button3: + trackrays(&xev.xbutton); + break; + } break; + case ClientMessage: + if ((xev.xclient.message_type == wmProtocolsAtom) && + (xev.xclient.data.l[0] == closedownAtom)) + quiterr(NULL); + break; } } -docom(ekey) /* execute command */ +traceray(xpos, ypos) /* print requested pixel data */ +int xpos, ypos; +{ + RREAL hv[2]; + FVECT rorg, rdir; + COLOR cval; + register char *cp; + + bbox.xmin = xpos; bbox.xsiz = 1; + bbox.ymin = ypos; bbox.ysiz = 1; + avgbox(cval); + scalecolor(cval, 1./exposure); + pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); + if (!gotview || viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) + rorg[0] = rorg[1] = rorg[2] = + rdir[0] = rdir[1] = rdir[2] = 0.; + + for (cp = tout; *cp; cp++) /* print what they asked for */ + switch (*cp) { + case 'o': /* origin */ + printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); + break; + case 'd': /* direction */ + printf("%e %e %e ", rdir[0], rdir[1], rdir[2]); + break; + case 'v': /* radiance value */ + printf("%e %e %e ", colval(cval,RED), + colval(cval,GRN), colval(cval,BLU)); + break; + case 'l': /* luminance */ + printf("%e ", luminance(cval)); + break; + case 'p': /* pixel position */ + printf("%d %d ", (int)(hv[0]*inpres.xr), + (int)(hv[1]*inpres.yr)); + break; + } + putchar('\n'); + fflush(stdout); + return(0); +} + + +docom(ekey) /* execute command */ XKeyPressedEvent *ekey; { char buf[80]; @@ -522,8 +677,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; - FLOAT hv[2]; - FVECT rorg, rdir; + RREAL hv[2]; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); if (n == 0) @@ -531,13 +685,14 @@ XKeyPressedEvent *ekey; com = buf[0]; switch (com) { /* interpret command */ case 'q': + case 'Q': case CTRL('D'): /* quit */ - quit(0); + quiterr(NULL); case '\n': case '\r': case 'l': case 'c': /* value */ - if (avgbox(cval) == -1) + if (!avgbox(cval)) return(-1); switch (com) { case '\n': @@ -545,7 +700,7 @@ XKeyPressedEvent *ekey; sprintf(buf, "%.3f", intens(cval)/exposure); break; case 'l': /* luminance */ - sprintf(buf, "%.0fL", luminance(cval)/exposure); + sprintf(buf, "%.1fL", luminance(cval)/exposure); break; case 'c': /* color */ comp = pow(2.0, (double)scale); @@ -556,7 +711,7 @@ XKeyPressedEvent *ekey; break; } XDrawImageString(thedisplay, wind, ourgc, - box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); + bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); return(0); case 'i': /* identify (contour) */ if (ourras->pixels == NULL) @@ -578,30 +733,49 @@ XKeyPressedEvent *ekey; buf, strlen(buf)); return(0); case 't': /* trace */ - if (!gotview) { - XBell(thedisplay, 0); + return(traceray(ekey->x, ekey->y)); + case 'a': /* auto exposure */ + if (fname == NULL) return(-1); - } - pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); - if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) + tmflags = TM_F_CAMERA; + strcpy(buf, "auto exposure..."); + goto remap; + case 'h': /* human response */ + if (fname == NULL) return(-1); - printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); - printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); - fflush(stdout); - return(0); + tmflags = TM_F_HUMAN; + strcpy(buf, "human exposure..."); + goto remap; case '=': /* adjust exposure */ - if (avgbox(cval) == -1) + case '@': /* adaptation level */ + if (!avgbox(cval)) return(-1); - n = log(.5/bright(cval))/.69315 - scale; /* truncate */ - if (n == 0) - return(0); - scale_rcolors(ourras, pow(2.0, (double)n)); + comp = bright(cval); + if (comp < 1e-20) { + XBell(thedisplay, 0); + return(-1); + } + if (com == '@') + comp = 106./exposure/ + pow(1.219+pow(comp*WHTEFFICACY/exposure,.4),2.5); + else + comp = .5/comp; + comp = log(comp)/.69315 - scale; + n = comp < 0 ? comp-.5 : comp+.5 ; /* round */ + if (tmflags != TM_F_LINEAR) + tmflags = TM_F_LINEAR; /* turn off tone mapping */ + else { + if (n == 0) /* else check if any change */ + return(0); + scale_rcolors(ourras, pow(2.0, (double)n)); + } scale += n; sprintf(buf, "%+d", scale); + remap: XDrawImageString(thedisplay, wind, ourgc, - box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); + bbox.xmin, bbox.ymin+bbox.ysiz, buf, strlen(buf)); XFlush(thedisplay); - free(ourdata); + /* free(ourdata); This is done in XDestroyImage()! */ free_raster(ourras); getras(); /* fall through */ @@ -614,8 +788,23 @@ XKeyPressedEvent *ekey; make_rpixmap(ourras, wind); redraw(0, 0, width, height); return(0); + case 'f': /* turn on fast redraw */ + fast = 1; + make_rpixmap(ourras, wind); + return(0); + case 'F': /* turn off fast redraw */ + fast = 0; + free_rpixmap(ourras); + return(0); + case '0': /* recenter origin */ + if ((xoff == 0) & (yoff == 0)) + return(0); + xoff = yoff = 0; + XClearWindow(thedisplay, wind); + redraw(0, 0, width, height); + return(0); case ' ': /* clear */ - redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); + redraw(bbox.xmin, bbox.ymin, bbox.xsiz, bbox.ysiz); return(0); default: XBell(thedisplay, 0); @@ -627,65 +816,76 @@ XKeyPressedEvent *ekey; moveimage(ebut) /* shift the image */ XButtonPressedEvent *ebut; { - union { - XEvent u; - XButtonReleasedEvent b; - XPointerMovedEvent m; - } e; + XEvent e; int mxo, myo; - XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); - while (e.u.type == MotionNotify) { - mxo = e.m.x; - myo = e.m.y; + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); + while (e.type == MotionNotify) { + mxo = e.xmotion.x; + myo = e.xmotion.y; revline(ebut->x, ebut->y, mxo, myo); revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); - XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); revline(ebut->x, ebut->y, mxo, myo); revbox(xoff+mxo-ebut->x, yoff+myo-ebut->y, xoff+mxo-ebut->x+xmax, yoff+myo-ebut->y+ymax); } - xoff += e.b.x - ebut->x; - yoff += e.b.y - ebut->y; + xoff += e.xbutton.x - ebut->x; + yoff += e.xbutton.y - ebut->y; XClearWindow(thedisplay, wind); redraw(0, 0, width, height); } -getbox(ebut) /* get new box */ +getbox(ebut) /* get new bbox */ XButtonPressedEvent *ebut; { - union { - XEvent u; - XButtonReleasedEvent b; - XPointerMovedEvent m; - } e; + XEvent e; - XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); - while (e.u.type == MotionNotify) { - revbox(ebut->x, ebut->y, box.xmin = e.m.x, box.ymin = e.m.y); - XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); - revbox(ebut->x, ebut->y, box.xmin, box.ymin); + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); + while (e.type == MotionNotify) { + revbox(ebut->x, ebut->y, bbox.xmin = e.xmotion.x, bbox.ymin = e.xmotion.y); + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); + revbox(ebut->x, ebut->y, bbox.xmin, bbox.ymin); } - box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); - box.ymin = e.b.y<0 ? 0 : (e.b.y>=height ? height-1 : e.b.y); - if (box.xmin > ebut->x) { - box.xsiz = box.xmin - ebut->x + 1; - box.xmin = ebut->x; + bbox.xmin = e.xbutton.x<0 ? 0 : (e.xbutton.x>=width ? width-1 : e.xbutton.x); + bbox.ymin = e.xbutton.y<0 ? 0 : (e.xbutton.y>=height ? height-1 : e.xbutton.y); + if (bbox.xmin > ebut->x) { + bbox.xsiz = bbox.xmin - ebut->x + 1; + bbox.xmin = ebut->x; } else { - box.xsiz = ebut->x - box.xmin + 1; + bbox.xsiz = ebut->x - bbox.xmin + 1; } - if (box.ymin > ebut->y) { - box.ysiz = box.ymin - ebut->y + 1; - box.ymin = ebut->y; + if (bbox.ymin > ebut->y) { + bbox.ysiz = bbox.ymin - ebut->y + 1; + bbox.ymin = ebut->y; } else { - box.ysiz = ebut->y - box.ymin + 1; + bbox.ysiz = ebut->y - bbox.ymin + 1; } } -revbox(x0, y0, x1, y1) /* draw box with reversed lines */ +trackrays(ebut) /* trace rays as mouse moves */ +XButtonPressedEvent *ebut; +{ + XEvent e; + unsigned long lastrept; + + traceray(ebut->x, ebut->y); + lastrept = ebut->time; + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e); + while (e.type == MotionNotify) { + if (e.xmotion.time >= lastrept + tinterv) { + traceray(e.xmotion.x, e.xmotion.y); + lastrept = e.xmotion.time; + } + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e); + } +} + + +revbox(x0, y0, x1, y1) /* draw bbox with reversed lines */ int x0, y0, x1, y1; { revline(x0, y0, x1, y0); @@ -695,54 +895,180 @@ int x0, y0, x1, y1; } -avgbox(clr) /* average color over current box */ -COLOR clr; +void +colavg(scn, n, cavg) +register COLR *scn; +register int n; +COLOR cavg; { - static COLOR lc; - static int ll, lr, lt, lb; + COLOR col; + + while (n--) { + colr_color(col, *scn++); + addcolor(cavg, col); + } +} + + +int +avgbox(cavg) /* average color over current bbox */ +COLOR cavg; +{ + double d; + register int rval; + + setcolor(cavg, 0., 0., 0.); + rval = dobox(colavg, (char *)cavg); + if (rval > 0) { + d = 1./rval; + scalecolor(cavg, d); + } + return(rval); +} + + +int +dobox(f, p) /* run function over bbox */ +void (*f)(); /* function to call for each subscan */ +char *p; /* pointer to private data */ +{ int left, right, top, bottom; int y; - double d; - COLOR ctmp; - register int x; - setcolor(clr, 0.0, 0.0, 0.0); - left = box.xmin - xoff; - right = left + box.xsiz; + left = bbox.xmin - xoff; + right = left + bbox.xsiz; if (left < 0) left = 0; if (right > xmax) right = xmax; if (left >= right) - return(-1); - top = box.ymin - yoff; - bottom = top + box.ysiz; + return(0); + top = bbox.ymin - yoff; + bottom = top + bbox.ysiz; if (top < 0) top = 0; if (bottom > ymax) bottom = ymax; if (top >= bottom) - return(-1); - if (left == ll && right == lr && top == lt && bottom == lb) { - copycolor(clr, lc); return(0); - } for (y = top; y < bottom; y++) { if (getscan(y) == -1) return(-1); - for (x = left; x < right; x++) { - colr_color(ctmp, scanline[x]); - addcolor(clr, ctmp); + (*f)(scanline+left, right-left, p); + } + return((right-left)*(bottom-top)); +} + + +void +addfix(scn, n) /* add fixation points to histogram */ +COLR *scn; +int n; +{ + if (tmCvColrs(lscan, TM_NOCHROM, scn, n)) + goto tmerr; + if (tmAddHisto(lscan, n, FIXWEIGHT)) + goto tmerr; + return; +tmerr: + quiterr("tone mapping error"); +} + + +make_tonemap() /* initialize tone mapping */ +{ + int flags, y; + + if (tmflags != TM_F_LINEAR && fname == NULL) { + fprintf(stderr, "%s: cannot adjust tone of standard input\n", + progname); + tmflags = TM_F_LINEAR; + } + if (tmflags == TM_F_LINEAR) { /* linear with clamping */ + setcolrcor(pow, 1.0/gamcor); + return; + } + flags = tmflags; /* histogram adjustment */ + if (greyscale) flags |= TM_F_BW; + if (tmTop != NULL) { /* reuse old histogram if one */ + tmDone(tmTop); + tmTop->flags = flags; + } else { /* else initialize */ + if ((lscan = (TMbright *)malloc(xmax*sizeof(TMbright))) == NULL) + goto memerr; + if (greyscale) { + cscan = TM_NOCHROM; + if ((pscan = (BYTE *)malloc(sizeof(BYTE)*xmax)) == NULL) + goto memerr; + } else if ((pscan=cscan = (BYTE *)malloc(3*sizeof(BYTE)*xmax)) + == NULL) + goto memerr; + /* initialize tm library */ + if (tmInit(flags, stdprims, gamcor) == NULL) + goto memerr; + if (tmSetSpace(stdprims, WHTEFFICACY/exposure)) + goto tmerr; + /* compute picture histogram */ + for (y = 0; y < ymax; y++) { + getscan(y); + if (tmCvColrs(lscan, TM_NOCHROM, scanline, xmax)) + goto tmerr; + if (tmAddHisto(lscan, xmax, 1)) + goto tmerr; } } - d = 1.0/((right-left)*(bottom-top)); - scalecolor(clr, d); - ll = left; lr = right; lt = top; lb = bottom; - copycolor(lc, clr); - return(0); + tmDup(); /* add fixations to duplicate map */ + dobox(addfix, NULL); + /* (re)compute tone mapping */ + if (tmComputeMapping(gamcor, 0., 0.)) + goto tmerr; + return; +memerr: + quiterr("out of memory in make_tonemap"); +tmerr: + quiterr("tone mapping error"); } +tmap_colrs(scn, len) /* apply tone mapping to scanline */ +register COLR *scn; +int len; +{ + register BYTE *ps; + + if (tmflags == TM_F_LINEAR) { + if (scale) + shiftcolrs(scn, len, scale); + colrs_gambs(scn, len); + return; + } + if (len > xmax) + quiterr("code error 1 in tmap_colrs"); + if (tmCvColrs(lscan, cscan, scn, len)) + goto tmerr; + if (tmMapPixels(pscan, lscan, cscan, len)) + goto tmerr; + ps = pscan; + if (greyscale) + while (len--) { + scn[0][RED] = scn[0][GRN] = scn[0][BLU] = *ps++; + scn[0][EXP] = COLXS; + scn++; + } + else + while (len--) { + scn[0][RED] = *ps++; + scn[0][GRN] = *ps++; + scn[0][BLU] = *ps++; + scn[0][EXP] = COLXS; + scn++; + } + return; +tmerr: + quiterr("tone mapping error"); +} + + getmono() /* get monochrome data */ { register unsigned char *dp; @@ -771,7 +1097,7 @@ getmono() /* get monochrome data */ cerr[x] = err + errp; } } - free((char *)cerr); + free((void *)cerr); } @@ -782,8 +1108,7 @@ COLR *scan; static short cerr[ICONSIZ]; static int ynext; static char *dp; - double sf; - COLOR col; + COLR clr; register int err; register int x, ti; int errp; @@ -808,17 +1133,15 @@ COLR *scan; } if (y < ynext*ymax/iconheight) /* skip this one */ return; - sf = pow(2.0, (double)(scale+8)); err = 0; for (x = 0; x < iconwidth; x++) { if (!(x&7)) *++dp = 0; errp = err; ti = x*xmax/iconwidth; - colr_color(col, scan[ti]); - ti = sf*bright(col); - if (ti > 255) ti = 255; - err += ti + cerr[x]; + copycolr(clr, scan[ti]); + normcolrs(&clr, 1, scale); + err += normbright(clr) + cerr[x]; if (err > 127) err -= 255; else @@ -833,28 +1156,66 @@ COLR *scan; getfull() /* get full (24-bit) data */ { int y; - register unsigned long *dp; + register uint32 *dp; + register uint16 *dph; register int x; - /* set gamma correction */ - setcolrgam(gamcor); + /* initialize tone mapping */ + make_tonemap(); /* read and convert file */ - dp = (unsigned long *)ourdata; + dp = (uint32 *)ourdata; + dph = (uint16 *)ourdata; for (y = 0; y < ymax; y++) { getscan(y); add2icon(y, scanline); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); - if (ourras->image->blue_mask & 1) + tmap_colrs(scanline, xmax); + switch (ourras->image->blue_mask) { + case 0xff: /* 24-bit RGB */ for (x = 0; x < xmax; x++) - *dp++ = scanline[x][RED] << 16 | - scanline[x][GRN] << 8 | - scanline[x][BLU] ; - else + *dp++ = (uint32)scanline[x][RED] << 16 | + (uint32)scanline[x][GRN] << 8 | + (uint32)scanline[x][BLU] ; + break; + case 0xff0000: /* 24-bit BGR */ for (x = 0; x < xmax; x++) - *dp++ = scanline[x][RED] | - scanline[x][GRN] << 8 | - scanline[x][BLU] << 16 ; + *dp++ = (uint32)scanline[x][RED] | + (uint32)scanline[x][GRN] << 8 | + (uint32)scanline[x][BLU] << 16 ; + break; +#if 0 + case 0x1f: /* 15-bit RGB */ + for (x = 0; x < xmax; x++) + *dph++ = (scanline[x][RED] << 7 & 0x7c00) | + (scanline[x][GRN] << 2 & 0x3e0) | + (unsigned)scanline[x][BLU] >> 3 ; + break; + case 0x7c00: /* 15-bit BGR */ + for (x = 0; x < xmax; x++) + *dph++ = (unsigned)scanline[x][RED] >> 3 | + (scanline[x][GRN] << 2 & 0x3e0) | + (scanline[x][BLU] << 7 & 0x7c00) ; + break; +#endif + default: /* unknown */ + if (ourvis.depth > 16) + for (x = 0; x < xmax; x++) { + *dp = ourras->image->red_mask & + ourras->image->red_mask*scanline[x][RED]/255; + *dp |= ourras->image->green_mask & + ourras->image->green_mask*scanline[x][GRN]/255; + *dp++ |= ourras->image->blue_mask & + ourras->image->blue_mask*scanline[x][BLU]/255; + } + else + for (x = 0; x < xmax; x++) { + *dph = ourras->image->red_mask & + ourras->image->red_mask*scanline[x][RED]/255; + *dph |= ourras->image->green_mask & + ourras->image->green_mask*scanline[x][GRN]/255; + *dph++ |= ourras->image->blue_mask & + ourras->image->blue_mask*scanline[x][BLU]/255; + } + break; + } } } @@ -864,55 +1225,52 @@ getgrey() /* get greyscale data */ int y; register unsigned char *dp; register int x; - /* set gamma correction */ - setcolrgam(gamcor); + /* initialize tone mapping */ + make_tonemap(); /* read and convert file */ dp = ourdata; for (y = 0; y < ymax; y++) { getscan(y); add2icon(y, scanline); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); + tmap_colrs(scanline, xmax); if (maxcolors < 256) for (x = 0; x < xmax; x++) - *dp++ = ((long)normbright(scanline[x]) * - maxcolors + 128) >> 8; + *dp++ = ((int32)scanline[x][GRN] * + maxcolors + maxcolors/2) >> 8; else for (x = 0; x < xmax; x++) - *dp++ = normbright(scanline[x]); + *dp++ = scanline[x][GRN]; } for (x = 0; x < maxcolors; x++) clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = ((long)x*256+maxcolors/2)/maxcolors; + clrtab[x][BLU] = ((int32)x*256 + 128)/maxcolors; } getmapped() /* get color-mapped data */ { int y; - /* set gamma correction */ - setcolrgam(gamcor); + /* make sure we can do it first */ + if (fname == NULL) + quiterr("cannot map colors from standard input"); + /* initialize tone mapping */ + make_tonemap(); /* make histogram */ - new_histo(); + if (new_histo((int32)xmax*ymax) == -1) + quiterr("cannot initialize histogram"); for (y = 0; y < ymax; y++) { if (getscan(y) < 0) - quiterr("seek error in getmapped"); + break; add2icon(y, scanline); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); + tmap_colrs(scanline, xmax); cnt_colrs(scanline, xmax); } /* map pixels */ if (!new_clrtab(maxcolors)) quiterr("cannot create color map"); for (y = 0; y < ymax; y++) { - if (getscan(y) < 0) - quiterr("seek error in getmapped"); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); + getscan(y); + tmap_colrs(scanline, xmax); if (dither) dith_colrs(ourdata+y*xmax, scanline, xmax); else @@ -952,6 +1310,12 @@ double sf; getscan(y) int y; { + static int trunced = -1; /* truncated file? */ +skipit: + if (trunced >= 0 && y >= trunced) { + memset(scanline, '\0', xmax*sizeof(COLR)); + return(-1); + } if (y != cury) { if (scanpos == NULL || scanpos[y] == -1) return(-1); @@ -961,9 +1325,12 @@ int y; } else if (scanpos != NULL && scanpos[y] == -1) scanpos[y] = ftell(fin); - if (freadcolrs(scanline, xmax, fin) < 0) - quiterr("read error"); - + if (freadcolrs(scanline, xmax, fin) < 0) { + fprintf(stderr, "%s: %s: unfinished picture\n", + progname, fname==NULL?"":fname); + trunced = y; + goto skipit; + } cury++; return(0); }