--- ray/src/px/x11image.c 1990/03/01 13:18:22 1.1 +++ ray/src/px/x11image.c 1990/03/12 15:14:53 1.6 @@ -43,6 +43,10 @@ static char SCCSid[] = "$SunId$ LBL"; #define ourroot RootWindow(thedisplay,ourscreen) #define ourgc DefaultGC(thedisplay,ourscreen) +#define revline(x0,y0,x1,y1) XDrawLine(thedisplay,wind,revgc,x0,y0,x1,y1) + +#define redraw(x,y,w,h) patch_raster(wind,(x)-xoff,(y)-yoff,x,y,w,h,ourras) + double gamcor = 2.2; /* gamma correction */ int dither = 1; /* dither colors? */ @@ -51,7 +55,7 @@ int fast = 0; /* keep picture in Pixmap? */ Window wind = 0; /* our output window */ Font fontid; /* our font */ -long maxcolors = 0; /* maximum colors */ +int maxcolors = 0; /* maximum colors */ int greyscale = 0; /* in grey */ int scale = 0; /* scalefactor; power of two */ @@ -73,6 +77,8 @@ int cury = 0; /* current scan location */ double exposure = 1.0; /* exposure compensation used */ +GC revgc; /* graphics context with GXinvert */ + XRASTER *ourras; /* our stored image */ unsigned char *ourdata; /* our image data */ @@ -135,17 +141,20 @@ char *argv[]; default: goto userr; } + else if (argv[i][0] == '=') + geometry = argv[i]; else break; - if (argc-i == 1) { + if (i == argc-1) { fname = argv[i]; fin = fopen(fname, "r"); if (fin == NULL) { sprintf(errmsg, "can't open file \"%s\"", fname); quiterr(errmsg); } - } + } else if (i != argc) + goto userr; /* get header */ getheader(fin, headline); /* get picture dimensions */ @@ -175,12 +184,12 @@ char *s; static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; register char **an; - if (!strncmp(s, "EXPOSURE=", 9)) - exposure *= atof(s+9); + if (isexpos(s)) + exposure *= exposval(s); else for (an = altname; *an != NULL; an++) if (!strncmp(*an, s, strlen(*an))) { - if (sscanview(&ourview, s+strlen(*an)) == 0) + if (sscanview(&ourview, s+strlen(*an)) > 0) gotview++; return; } @@ -189,6 +198,7 @@ char *s; init() /* get data and open window */ { + XSetWindowAttributes ourwinattr; XSizeHints oursizhints; register int i; @@ -201,18 +211,29 @@ init() /* get data and open window */ } if ((thedisplay = XOpenDisplay(NULL)) == NULL) quiterr("can't open display; DISPLAY variable set?"); - wind = XCreateSimpleWindow(thedisplay, - ourroot, 0, 0, xmax, ymax, BORWIDTH, ourblack, ourwhite); - if (wind == 0) - quiterr("can't create window"); if (maxcolors == 0) { /* get number of available colors */ - maxcolors = 1< 8 ? 256 : 1< 4) maxcolors -= 2; } + /* store image */ + getras(); + /* open window */ + ourwinattr.border_pixel = ourblack; + ourwinattr.background_pixel = ourwhite; + wind = XCreateWindow(thedisplay, ourroot, 0, 0, xmax, ymax, BORWIDTH, + 0, InputOutput, ourras->visual, + CWBackPixel|CWBorderPixel, &ourwinattr); + if (wind == 0) + quiterr("can't create window"); + width = xmax; + height = ymax; fontid = XLoadFont(thedisplay, FONTNAME); if (fontid == 0) quiterr("can't get font"); XSetFont(thedisplay, ourgc, fontid); + revgc = XCreateGC(thedisplay, wind, 0, 0); + XSetFunction(thedisplay, revgc, GXinvert); XStoreName(thedisplay, wind, fname == NULL ? progname : fname); XDefineCursor(thedisplay, wind, XCreateFontCursor(thedisplay, XC_diamond_cross)); @@ -220,22 +241,27 @@ init() /* get data and open window */ bzero((char *)&oursizhints, sizeof(oursizhints)); i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, &oursizhints.width, &oursizhints.height); - if (i & (XValue|YValue) == (XValue|YValue)) { + if ((i&(WidthValue|HeightValue)) == (WidthValue|HeightValue)) + oursizhints.flags |= USSize; + else { + oursizhints.width = xmax; + oursizhints.height = ymax; + oursizhints.flags |= PSize; + } + if ((i&(XValue|YValue)) == (XValue|YValue)) { oursizhints.flags |= USPosition; if (i & XNegative) - oursizhints.x += DisplayWidth(thedisplay,ourscreen)-1; + oursizhints.x += DisplayWidth(thedisplay, + ourscreen)-1-oursizhints.width-2*BORWIDTH; if (i & YNegative) - oursizhints.y += DisplayHeight(thedisplay,ourscreen)-1; + oursizhints.y += DisplayHeight(thedisplay, + ourscreen)-1-oursizhints.height-2*BORWIDTH; } - if (i & (WidthValue|HeightValue) == (WidthValue|HeightValue)) - oursizhints.flags |= USSize; XSetNormalHints(thedisplay, wind, &oursizhints); } - /* store image */ - getras(); - XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask| - StructureNotifyMask|ButtonMotionMask| - KeyPressMask|ExposureMask); + XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask + |ButtonMotionMask|StructureNotifyMask + |KeyPressMask|ExposureMask); XMapWindow(thedisplay, wind); return; memerr: @@ -271,9 +297,7 @@ int code; getras() /* get raster file */ { colormap ourmap; - unsigned char rmap[256], gmap[256], bmap[256]; XVisualInfo vinfo; - register int i; if (maxcolors <= 2) { /* monochrome */ ourdata = (unsigned char *)malloc(ymax*((xmax+7)/8)); @@ -305,12 +329,7 @@ getras() /* get raster file */ biq(dither,maxcolors,1,ourmap); else ciq(dither,maxcolors,1,ourmap); - for (i = 0; i < 256; i++) { - rmap[i] = ourmap[0][i]; - gmap[i] = ourmap[1][i]; - bmap[i] = ourmap[2][i]; - } - if (init_rcolors(ourras, rmap, gmap, bmap) == 0) + if (init_rcolors(ourras, ourmap[0], ourmap[1], ourmap[2]) == 0) goto fail; } return; @@ -359,14 +378,6 @@ getevent() /* process the next event */ } -redraw(x, y, w, h) /* redraw section of window */ -int x, y; -int w, h; -{ - patch_raster(wind,x-xoff,y-yoff,x,y,w,h,ourras); -} - - docom(ekey) /* execute command */ XKeyPressedEvent *ekey; { @@ -407,8 +418,8 @@ XKeyPressedEvent *ekey; colval(cval,BLU)*comp); break; } - XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, - buf, strlen(buf)); + XDrawImageString(thedisplay, wind, ourgc, + box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); return(0); case 'i': /* identify (contour) */ if (ourras->pixels == NULL) @@ -419,7 +430,8 @@ XKeyPressedEvent *ekey; cvx.red = random() & 65535; cvx.green = random() & 65535; cvx.blue = random() & 65535; - XStoreColors(thedisplay, ourras->cmap, &cvx, 1); + cvx.flags = DoRed|DoGreen|DoBlue; + XStoreColor(thedisplay, ourras->cmap, &cvx); return(0); case 'p': /* position */ sprintf(buf, "(%d,%d)", ekey->x-xoff, ymax-1-ekey->y+yoff); @@ -447,8 +459,8 @@ XKeyPressedEvent *ekey; scale_rcolors(ourras, pow(2.0, (double)n)); scale += n; sprintf(buf, "%+d", scale); - XDrawImageString(thedisplay, wind, ourgc, box.xmin, box.ymin, - buf, strlen(buf)); + XDrawImageString(thedisplay, wind, ourgc, + box.xmin, box.ymin+box.ysiz, buf, strlen(buf)); XFlush(thedisplay); free(ourdata); free_raster(ourras); @@ -467,23 +479,38 @@ XKeyPressedEvent *ekey; redraw(box.xmin, box.ymin, box.xsiz, box.ysiz); return(0); default: - XBell(0); + XBell(thedisplay, 0); return(-1); } } -moveimage(ep) /* shift the image */ -XButtonPressedEvent *ep; +moveimage(ebut) /* shift the image */ +XButtonPressedEvent *ebut; { - XButtonPressedEvent eb; + union { + XEvent u; + XButtonReleasedEvent b; + XPointerMovedEvent m; + } e; + int mxo, myo; - XMaskEvent(thedisplay, ButtonReleaseMask, &eb); - xoff += eb.x - ep->x; - yoff += eb.y - ep->y; + XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.u); + while (e.u.type == MotionNotify) { + mxo = e.m.x; + myo = e.m.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); + 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; XClearWindow(thedisplay, wind); redraw(0, 0, width, height); - return(0); } @@ -491,16 +518,15 @@ getbox(ebut) /* get new box */ XButtonPressedEvent *ebut; { union { - XEvent e; + XEvent u; XButtonReleasedEvent b; XPointerMovedEvent m; } e; - XMaskEvent(thedisplay, ButtonReleaseMask|ButtonMotionMask, &e.e); - while (e.e.type == ButtonMotionMask) { + 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.e); + XMaskEvent(thedisplay,ButtonReleaseMask|ButtonMotionMask,&e.u); revbox(ebut->x, ebut->y, box.xmin, box.ymin); } box.xmin = e.b.x<0 ? 0 : (e.b.x>=width ? width-1 : e.b.x); @@ -523,20 +549,13 @@ XButtonPressedEvent *ebut; revbox(x0, y0, x1, y1) /* draw box with reversed lines */ int x0, y0, x1, y1; { - static GC mygc = 0; + revline(x0, y0, x1, y0); + revline(x0, y1, x1, y1); + revline(x0, y0, x0, y1); + revline(x1, y0, x1, y1); +} - if (mygc == 0) { - mygc = XCreateGC(thedisplay, wind, 0, 0); - XSetPlaneMask(thedisplay, mygc, ~0L); - XSetFunction(thedisplay, mygc, GXinvert); - } - XDrawLine(thedisplay, wind, mygc, x0, y0, x1, y0); - XDrawLine(thedisplay, wind, mygc, x0, y1, x1, y1); - XDrawLine(thedisplay, wind, mygc, x0, y0, x0, y1); - XDrawLine(thedisplay, wind, mygc, x1, y0, x1, y1); -} /* end of revbox */ - avgbox(clr) /* average color over current box */ COLOR clr; { @@ -582,28 +601,28 @@ getmono() /* get monochrome data */ register unsigned char *dp; register int x, err; int y; - rgbpixel *inline; + rgbpixel *inl; short *cerr; - if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL + if ((inl = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) quit("out of memory in getmono"); dp = ourdata - 1; for (y = 0; y < ymax; y++) { - picreadline3(y, inline); + picreadline3(y, inl); err = 0; for (x = 0; x < xmax; x++) { if (!(x&7)) *++dp = 0; - err += rgb_bright(&inline[x]) + cerr[x]; + err += rgb_bright(&inl[x]) + cerr[x]; if (err > 127) err -= 255; else - *dp |= 1<<(x&07); + *dp |= 1<<(7-(x&07)); cerr[x] = err >>= 1; } } - free((char *)inline); + free((char *)inl); free((char *)cerr); }