--- ray/src/px/x11image.c 1993/10/27 16:57:28 2.30 +++ ray/src/px/x11image.c 1997/07/31 21:00:57 2.50 @@ -1,7 +1,7 @@ -/* Copyright (c) 1993 Regents of the University of California */ +/* Copyright (c) 1997 Silicon Graphics, Inc. */ #ifndef lint -static char SCCSid[] = "$SunId$ LBL"; +static char SCCSid[] = "$SunId$ SGI"; #endif /* @@ -27,17 +27,16 @@ static char SCCSid[] = "$SunId$ LBL"; #include #include -#include "color.h" +#undef NOPROTO +#define NOPROTO 1 +#include "tonemap.h" #include "view.h" #include "x11raster.h" #include "random.h" #include "resolu.h" -#ifdef __alpha -#define int4 int -#endif #ifndef int4 -#define int4 long +#define int4 int /* most int's are 32-bit */ #endif #define FONTNAME "8x13" /* text font we'll use */ @@ -48,6 +47,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) @@ -56,6 +57,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? */ @@ -73,11 +75,20 @@ 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 */ @@ -112,61 +123,80 @@ 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 *getenv(); + Display *thedisplay; Atom closedownAtom, wmProtocolsAtom; -int noop() {} +int sigrecv; +int onsig() { sigrecv++; } + 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); 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; @@ -179,6 +209,8 @@ char *argv[]; 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; @@ -186,17 +218,15 @@ char *argv[]; if (pid < 0) quiterr("fork failed"); parent++; - signal(SIGCONT, noop); - pause(); /* wait for wake-up call */ + 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); - } + if (fin == NULL) + quiterr("cannot open picture file"); } /* get header */ getheader(fin, headline, NULL); @@ -213,14 +243,11 @@ char *argv[]; init(argc, argv); /* get file and open window */ - if (parent < 0) - kill(getppid(), SIGCONT); /* signal parent if child */ - for ( ; ; ) getevent(); /* main loop */ userr: fprintf(stderr, -"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e +/-stops] pic ..\n", +"Usage: %s [-di disp][[-ge] spec][-b][-m][-d][-f][-c nclrs][-e spec][-g gamcor][-s][-ospec][-t intvl] pic ..\n", progname); exit(1); } @@ -233,10 +260,9 @@ char *s; 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++; } @@ -270,6 +296,13 @@ char **argv; 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 */ @@ -296,25 +329,29 @@ char **argv; } } /* open window */ + i = CWEventMask|CWCursor|CWBackPixel|CWBorderPixel; ourwinattr.border_pixel = ourwhite; ourwinattr.background_pixel = ourblack; - ourwinattr.colormap = XCreateColormap(thedisplay, ourroot, - ourvis.visual, AllocNone); + 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, CWEventMask| - CWCursor|CWBackPixel|CWBorderPixel|CWColormap, &ourwinattr); + ourvis.depth, InputOutput, ourvis.visual, + i, &ourwinattr); if (wind == 0) quiterr("cannot create window"); width = xmax; height = ymax; - xgcv.foreground = ourblack; - xgcv.background = ourwhite; + /* 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; @@ -343,23 +380,28 @@ char **argv; XSetWMProtocols(thedisplay, wind, &closedownAtom, 1); XMapWindow(thedisplay, wind); - return; } /* end of init */ quiterr(err) /* print message and exit */ char *err; { - if (err != NULL) + register int es; + int cs; + + if (es = err != NULL) fprintf(stderr, "%s: %s: %s\n", progname, fname==NULL?"":fname, err); - if (wind) { - XDestroyWindow(thedisplay, wind); - XFlush(thedisplay); - } - while (parent > 0 && wait(0) != -1) /* wait for any children */ + 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(err != NULL); + } + exit(es); } @@ -516,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(); @@ -546,6 +588,10 @@ getevent() /* process the next event */ 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) @@ -558,10 +604,18 @@ getevent() /* process the next event */ case ButtonPress: if (xev.xbutton.state & (ShiftMask|ControlMask)) moveimage(&xev.xbutton); - else if (xev.xbutton.button == Button2) - traceray(xev.xbutton.x, xev.xbutton.y); else - getbox(&xev.xbutton); + 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) && @@ -572,21 +626,45 @@ getevent() /* process the next event */ } -traceray(xpos, ypos) /* print ray corresponding to pixel */ +traceray(xpos, ypos) /* print requested pixel data */ int xpos, ypos; { + extern char *index(); FLOAT hv[2]; FVECT rorg, rdir; + COLOR cval; + register char *cp; - if (!gotview) { /* no view, no can do */ - XBell(thedisplay, 0); - return(-1); - } + box.xmin = xpos; box.xsiz = 1; + box.ymin = ypos; box.ysiz = 1; + avgbox(cval); + scalecolor(cval, 1./exposure); pix2loc(hv, &inpres, xpos-xoff, ypos-yoff); - if (viewray(rorg, rdir, &ourview, hv[0], hv[1]) < 0) - return(-1); - printf("%e %e %e ", rorg[0], rorg[1], rorg[2]); - printf("%e %e %e\n", rdir[0], rdir[1], rdir[2]); + 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); } @@ -615,7 +693,7 @@ XKeyPressedEvent *ekey; case '\r': case 'l': case 'c': /* value */ - if (avgbox(cval) == -1) + if (!avgbox(cval)) return(-1); switch (com) { case '\n': @@ -657,20 +735,40 @@ XKeyPressedEvent *ekey; return(0); case 't': /* trace */ return(traceray(ekey->x, ekey->y)); + case 'a': /* auto exposure */ + tmflags = TM_F_CAMERA; + strcpy(buf, "auto exposure..."); + goto remap; + case 'h': /* human response */ + tmflags = TM_F_HUMAN; + strcpy(buf, "human exposure..."); + goto remap; case '=': /* adjust exposure */ case '@': /* adaptation level */ - if (avgbox(cval) == -1) + if (!avgbox(cval)) return(-1); - comp = com=='@' - ? 106./pow(1.219+pow(luminance(cval)/exposure,.4),2.5)/exposure - : .5/bright(cval) ; + 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 (n == 0) - return(0); - scale_rcolors(ourras, pow(2.0, (double)n)); + 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)); XFlush(thedisplay); @@ -765,6 +863,25 @@ XButtonPressedEvent *ebut; } +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 box with reversed lines */ int x0, y0, x1, y1; { @@ -775,18 +892,46 @@ int x0, y0, x1, y1; } -avgbox(clr) /* average color over current box */ -COLOR clr; +int +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 box */ +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 box */ +int (*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; if (left < 0) @@ -794,7 +939,7 @@ COLOR clr; if (right > xmax) right = xmax; if (left >= right) - return(-1); + return(0); top = box.ymin - yoff; bottom = top + box.ysiz; if (top < 0) @@ -802,27 +947,125 @@ COLOR clr; 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)); +} + + +int +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; @@ -912,26 +1155,24 @@ getfull() /* get full (24-bit) data */ int y; register unsigned int4 *dp; register int x; - /* set gamma correction */ - setcolrgam(gamcor); + /* initialize tone mapping */ + make_tonemap(); /* read and convert file */ dp = (unsigned int4 *)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 (ourras->image->blue_mask & 1) for (x = 0; x < xmax; x++) - *dp++ = scanline[x][RED] << 16 | - scanline[x][GRN] << 8 | - scanline[x][BLU] ; + *dp++ = (unsigned int4)scanline[x][RED] << 16 | + (unsigned int4)scanline[x][GRN] << 8 | + (unsigned int4)scanline[x][BLU] ; else for (x = 0; x < xmax; x++) - *dp++ = scanline[x][RED] | - scanline[x][GRN] << 8 | - scanline[x][BLU] << 16 ; + *dp++ = (unsigned int4)scanline[x][RED] | + (unsigned int4)scanline[x][GRN] << 8 | + (unsigned int4)scanline[x][BLU] << 16 ; } } @@ -941,21 +1182,17 @@ 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); - for (x = 0; x < xmax; x++) - scanline[x][GRN] = normbright(scanline[x]); - colrs_gambs(scanline, xmax); + tmap_colrs(scanline, xmax); if (maxcolors < 256) for (x = 0; x < xmax; x++) - *dp++ = ((long)scanline[x][GRN] * + *dp++ = ((int4)scanline[x][GRN] * maxcolors + maxcolors/2) >> 8; else for (x = 0; x < xmax; x++) @@ -963,7 +1200,7 @@ getgrey() /* get greyscale data */ } for (x = 0; x < maxcolors; x++) clrtab[x][RED] = clrtab[x][GRN] = - clrtab[x][BLU] = ((long)x*256 + 128)/maxcolors; + clrtab[x][BLU] = ((int4)x*256 + 128)/maxcolors; } @@ -973,17 +1210,16 @@ getmapped() /* get color-mapped data */ /* make sure we can do it first */ if (fname == NULL) quiterr("cannot map colors from standard input"); - /* set gamma correction */ - setcolrgam(gamcor); + /* initialize tone mapping */ + make_tonemap(); /* make histogram */ - new_histo(); + if (new_histo((int4)xmax*ymax) == -1) + quiterr("cannot initialize histogram"); for (y = 0; y < ymax; y++) { if (getscan(y) < 0) break; add2icon(y, scanline); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); + tmap_colrs(scanline, xmax); cnt_colrs(scanline, xmax); } /* map pixels */ @@ -991,9 +1227,7 @@ getmapped() /* get color-mapped data */ quiterr("cannot create color map"); for (y = 0; y < ymax; y++) { getscan(y); - if (scale) - shiftcolrs(scanline, xmax, scale); - colrs_gambs(scanline, xmax); + tmap_colrs(scanline, xmax); if (dither) dith_colrs(ourdata+y*xmax, scanline, xmax); else