--- ray/src/px/x11image.c 1991/05/03 12:01:44 1.21 +++ ray/src/px/x11image.c 1992/04/28 09:40:32 2.4 @@ -1,4 +1,4 @@ -/* Copyright (c) 1990 Regents of the University of California */ +/* Copyright (c) 1991 Regents of the University of California */ #ifndef lint static char SCCSid[] = "$SunId$ LBL"; @@ -30,10 +30,11 @@ static char SCCSid[] = "$SunId$ LBL"; #include "pic.h" #include "x11raster.h" #include "random.h" +#include "resolu.h" #define FONTNAME "8x13" /* text font we'll use */ -#define CTRL(c) ('c'-'@') +#define CTRL(c) ((c)-'@') #define BORWIDTH 5 /* border width */ @@ -70,7 +71,8 @@ int gotview = 0; /* got parameters from file */ COLR *scanline; /* scan line buffer */ -int xmax, ymax; /* picture resolution */ +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 */ @@ -81,8 +83,6 @@ double exposure = 1.0; /* exposure compensation use int wrongformat = 0; /* input in another format? */ -int imready = 0; /* is image up? */ - GC revgc; /* graphics context with GXinvert */ XRASTER *ourras; /* our stored image */ @@ -105,7 +105,7 @@ extern long ftell(); extern char *malloc(), *calloc(); -extern double atof(), pow(), log(); +extern double pow(), log(); Display *thedisplay; @@ -167,8 +167,10 @@ char *argv[]; /* get header */ getheader(fin, headline, NULL); /* get picture dimensions */ - if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) + if (wrongformat || !fgetsresolu(&inpres, fin)) quiterr("bad picture format"); + xmax = scanlen(&inpres); + ymax = numscans(&inpres); /* set view parameters */ if (gotview && setview(&ourview) != NULL) gotview = 0; @@ -190,8 +192,6 @@ userr: headline(s) /* get relevant info from header */ char *s; { - static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; - register char **an; char fmt[32]; if (isexpos(s)) @@ -199,13 +199,8 @@ char *s; else if (isformat(s)) { formatval(fmt, s); wrongformat = strcmp(fmt, COLRFMT); - } else - for (an = altname; *an != NULL; an++) - if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) > 0) - gotview++; - return; - } + } else if (isview(s) && sscanview(&ourview, s) > 0) + gotview++; } @@ -250,6 +245,7 @@ init() /* get data and open window */ XSetFunction(thedisplay, revgc, GXinvert); XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, XC_diamond_cross)); + XStoreName(thedisplay, wind, fname == NULL ? progname : fname); if (geometry != NULL) { bzero((char *)&oursizhints, sizeof(oursizhints)); i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, @@ -282,12 +278,6 @@ init() /* get data and open window */ |ButtonMotionMask|StructureNotifyMask |KeyPressMask|ExposureMask); XMapWindow(thedisplay, wind); - /* make sure the image is up */ - do - getevent(); - while (!imready); - /* store name as ready signal */ - XStoreName(thedisplay, wind, fname == NULL ? progname : fname); return; memerr: quiterr("out of memory"); @@ -394,7 +384,6 @@ getevent() /* process the next event */ break; case Expose: redraw(e.e.x, e.e.y, e.e.width, e.e.height); - imready++; break; case ButtonPress: if (e.b.state & (ShiftMask|ControlMask)) @@ -414,6 +403,7 @@ XKeyPressedEvent *ekey; XColor cvx; int com, n; double comp; + FLOAT hv[2]; FVECT rorg, rdir; n = XLookupString(ekey, buf, sizeof(buf), NULL, NULL); @@ -422,7 +412,7 @@ XKeyPressedEvent *ekey; com = buf[0]; switch (com) { /* interpret command */ case 'q': - case CTRL(D): /* quit */ + case CTRL('D'): /* quit */ quit(0); case '\n': case '\r': @@ -462,7 +452,9 @@ XKeyPressedEvent *ekey; XStoreColor(thedisplay, ourras->cmap, &cvx); return(0); case 'p': /* position */ - sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); + pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-yoff); + sprintf(buf, "(%d,%d)", (int)(hv[0]*inpres.xr), + (int)(hv[1]*inpres.yr)); XDrawImageString(thedisplay, wind, ourgc, ekey->x, ekey->y, buf, strlen(buf)); return(0); @@ -471,9 +463,8 @@ XKeyPressedEvent *ekey; XBell(thedisplay, 0); return(-1); } - if (viewray(rorg, rdir, &ourview, - (ekey->x-xoff+.5)/xmax, - (ymax-1-ekey->y+yoff+.5)/ymax) < 0) + pix2loc(hv, &inpres, ekey->x-xoff, ekey->y-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]); @@ -495,8 +486,8 @@ XKeyPressedEvent *ekey; free_raster(ourras); getras(); /* fall through */ - case CTRL(R): /* redraw */ - case CTRL(L): + case CTRL('R'): /* redraw */ + case CTRL('L'): unmap_rcolors(ourras); XClearWindow(thedisplay, wind); map_rcolors(ourras, wind); @@ -588,6 +579,8 @@ int x0, y0, x1, y1; avgbox(clr) /* average color over current box */ COLOR clr; { + static COLOR lc; + static int ll, lr, lt, lb; int left, right, top, bottom; int y; double d; @@ -611,6 +604,10 @@ COLOR clr; bottom = ymax; if (top >= bottom) return(-1); + if (left == ll && right == lr && top == lt && bottom == lb) { + copycolor(clr, lc); + return; + } for (y = top; y < bottom; y++) { if (getscan(y) == -1) return(-1); @@ -621,6 +618,8 @@ COLOR clr; } d = 1.0/((right-left)*(bottom-top)); scalecolor(clr, d); + ll = left; lr = right; lt = top; lb = bottom; + copycolor(lc, clr); return(0); } @@ -629,7 +628,7 @@ getmono() /* get monochrome data */ { register unsigned char *dp; register int x, err; - int y; + int y, errp; short *cerr; if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) @@ -644,12 +643,14 @@ getmono() /* get monochrome data */ for (x = 0; x < xmax; x++) { if (!(x&7)) *++dp = 0; + errp = err; err += normbright(scanline[x]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(7-(x&07)); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } } free((char *)cerr); @@ -665,6 +666,7 @@ COLR *scan; static char *dp; register int err; register int x, ti; + int errp; if (iconheight == 0) { /* initialize */ if (xmax <= ICONSIZ && ymax <= ICONSIZ) { @@ -673,8 +675,12 @@ COLR *scan; } else if (xmax > ymax) { iconwidth = ICONSIZ; iconheight = ICONSIZ*ymax/xmax; + if (iconheight < 1) + iconheight = 1; } else { iconwidth = ICONSIZ*xmax/ymax; + if (iconwidth < 1) + iconwidth = 1; iconheight = ICONSIZ; } ynext = 0; @@ -686,13 +692,15 @@ COLR *scan; for (x = 0; x < iconwidth; x++) { if (!(x&7)) *++dp = 0; + errp = err; ti = x*xmax/iconwidth; err += normbright(scan[ti]) + cerr[x]; if (err > 127) err -= 255; else *dp |= 1<<(x&07); - cerr[x] = err >>= 1; + err /= 3; + cerr[x] = err + errp; } ynext++; }