--- ray/src/px/x11image.c 1990/03/01 18:13:31 1.2 +++ ray/src/px/x11image.c 1991/05/02 17:24:54 1.17 @@ -37,12 +37,16 @@ static char SCCSid[] = "$SunId$ LBL"; #define BORWIDTH 5 /* border width */ +#define ICONSIZ 80 /* maximum icon dimension */ + #define ourscreen DefaultScreen(thedisplay) #define ourblack BlackPixel(thedisplay,ourscreen) #define ourwhite WhitePixel(thedisplay,ourscreen) #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 */ @@ -75,6 +79,10 @@ int cury = 0; /* current scan location */ double exposure = 1.0; /* exposure compensation used */ +int wrongformat = 0; /* input in another format? */ + +GC revgc; /* graphics context with GXinvert */ + XRASTER *ourras; /* our stored image */ unsigned char *ourdata; /* our image data */ @@ -84,6 +92,9 @@ struct { char *geometry = NULL; /* geometry specification */ +char icondata[(ICONSIZ+7)/8*ICONSIZ]; /* icon bitmap data */ +int iconwidth = 0, iconheight = 0; + char *progname; char errmsg[128]; @@ -137,22 +148,25 @@ 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); + getheader(fin, headline, NULL); /* get picture dimensions */ - if (fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) - quiterr("bad picture size"); + if (wrongformat || fgetresolu(&xmax, &ymax, fin) != (YMAJOR|YDECR)) + quiterr("bad picture format"); /* set view parameters */ if (gotview && setview(&ourview) != NULL) gotview = 0; @@ -176,10 +190,14 @@ char *s; { static char *altname[] = {"rview","rpict","pinterp",VIEWSTR,NULL}; register char **an; + char fmt[32]; if (isexpos(s)) exposure *= exposval(s); - else + 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) @@ -191,6 +209,8 @@ char *s; init() /* get data and open window */ { + XWMHints ourxwmhints; + XSetWindowAttributes ourwinattr; XSizeHints oursizhints; register int i; @@ -203,39 +223,60 @@ 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 */ i = DisplayPlanes(thedisplay,ourscreen); maxcolors = i > 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)); if (geometry != NULL) { bzero((char *)&oursizhints, sizeof(oursizhints)); i = XParseGeometry(geometry, &oursizhints.x, &oursizhints.y, - &oursizhints.width, &oursizhints.height); - if (i & (XValue|YValue) == (XValue|YValue)) { + (unsigned *)&oursizhints.width, + (unsigned *)&oursizhints.height); + 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(); + ourxwmhints.flags = InputHint|IconPixmapHint; + ourxwmhints.input = True; + ourxwmhints.icon_pixmap = XCreateBitmapFromData(thedisplay, + wind, icondata, iconwidth, iconheight); + XSetWMHints(thedisplay, wind, &ourxwmhints); XSelectInput(thedisplay, wind, ButtonPressMask|ButtonReleaseMask |ButtonMotionMask|StructureNotifyMask |KeyPressMask|ExposureMask); @@ -274,9 +315,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)); @@ -287,7 +326,9 @@ getras() /* get raster file */ if (ourras == NULL) goto fail; getmono(); - } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo)) { + } else if (XMatchVisualInfo(thedisplay,ourscreen,24,TrueColor,&vinfo) + /* kludge for DirectColor */ + || XMatchVisualInfo(thedisplay,ourscreen,24,DirectColor,&vinfo)) { ourdata = (unsigned char *)malloc(xmax*ymax*3); if (ourdata == NULL) goto fail; @@ -308,17 +349,12 @@ 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; fail: - quit("could not create raster image"); + quiterr("could not create raster image"); } @@ -392,7 +428,7 @@ XKeyPressedEvent *ekey; sprintf(buf, "%.3f", intens(cval)/exposure); break; case 'l': /* luminance */ - sprintf(buf, "%.0fn", bright(cval)*683.0/exposure); + sprintf(buf, "%.0fL", luminance(cval)/exposure); break; case 'c': /* color */ comp = pow(2.0, (double)scale); @@ -402,8 +438,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) @@ -427,9 +463,10 @@ XKeyPressedEvent *ekey; XBell(thedisplay, 0); return(-1); } - viewray(rorg, rdir, &ourview, + if (viewray(rorg, rdir, &ourview, (ekey->x-xoff+.5)/xmax, - (ymax-1-ekey->y+yoff+.5)/ymax); + (ymax-1-ekey->y+yoff+.5)/ymax) < 0) + 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); @@ -443,8 +480,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); @@ -473,11 +510,24 @@ moveimage(ebut) /* shift the image */ XButtonPressedEvent *ebut; { union { - XEvent u; - XButtonReleasedEvent b; - } e; + XEvent u; + XButtonReleasedEvent b; + XPointerMovedEvent m; + } e; + int mxo, myo; - XMaskEvent(thedisplay, ButtonReleaseMask, &e.u); + 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); @@ -520,19 +570,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); - 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; { @@ -578,38 +622,97 @@ getmono() /* get monochrome data */ register unsigned char *dp; register int x, err; int y; - rgbpixel *inline; short *cerr; - if ((inline = (rgbpixel *)malloc(xmax*sizeof(rgbpixel))) == NULL - || (cerr = (short *)calloc(xmax,sizeof(short))) == NULL) - quit("out of memory in getmono"); + if ((cerr = (short *)calloc(xmax,sizeof(short))) == NULL) + quiterr("out of memory in getmono"); dp = ourdata - 1; for (y = 0; y < ymax; y++) { - picreadline3(y, inline); + if (getscan(y) < 0) + quiterr("seek error in getmono"); + normcolrs(scanline, xmax, scale); + add2icon(y, scanline); err = 0; for (x = 0; x < xmax; x++) { if (!(x&7)) *++dp = 0; - err += rgb_bright(&inline[x]) + cerr[x]; + err += normbright(scanline[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 *)cerr); } +add2icon(y, scan) /* add a scanline to our icon data */ +int y; +COLR *scan; +{ + static char *dp = NULL; + static short cerr[ICONSIZ]; + register int err; + register int x, xi; + + if (iconheight == 0) { /* initialize */ + if (xmax < ICONSIZ && ymax < ICONSIZ) { + iconwidth = xmax; + iconheight = ymax; + } else if (xmax > ymax) { + iconwidth = ICONSIZ; + iconheight = ICONSIZ*ymax/xmax; + } else { + iconwidth = ICONSIZ*xmax/ymax; + iconheight = ICONSIZ; + } + dp = icondata - 1; + } + if (dp == NULL) /* done already */ + return; + if (y % (ymax/iconheight)) /* skip this one */ + return; + err = 0; + for (x = 0; x < iconwidth; x++) { + if (!(x&7)) + *++dp = 0; + xi = x*xmax/iconwidth; + err += normbright(scan[xi]) + cerr[x]; + if (err > 127) + err -= 255; + else + *dp |= 1<<(x&07); + cerr[x] = err >>= 1; + } + if (y >= ymax-ymax/iconheight) /* all done */ + dp = NULL; +} + + getfull() /* get full (24-bit) data */ { int y; - - for (y = 0; y < ymax; y++) - picreadline3(y, (rgbpixel *)(ourdata+y*xmax*3)); + register unsigned char *dp; + register int x; + /* set gamma correction */ + setcolrgam(gamcor); + /* read and convert file */ + dp = ourdata; + for (y = 0; y < ymax; y++) { + if (getscan(y) < 0) + quiterr("seek error in getfull"); + if (scale) + shiftcolrs(scanline, xmax, scale); + colrs_gambs(scanline, xmax); + add2icon(y, scanline); + for (x = 0; x < xmax; x++) { + *dp++ = scanline[x][RED]; + *dp++ = scanline[x][GRN]; + *dp++ = scanline[x][BLU]; + } + } } @@ -648,7 +751,7 @@ int y; if (scanpos == NULL || scanpos[y] == -1) return(-1); if (fseek(fin, scanpos[y], 0) == -1) - quit("fseek error"); + quiterr("fseek error"); cury = y; } else if (scanpos != NULL) scanpos[y] = ftell(fin); @@ -671,6 +774,7 @@ register rgbpixel *l3; quiterr("cannot seek for picreadline"); /* convert scanline */ normcolrs(scanline, xmax, scale); + add2icon(y, scanline); for (i = 0; i < xmax; i++) { l3[i].r = scanline[i][RED]; l3[i].g = scanline[i][GRN]; @@ -694,7 +798,7 @@ colormap map; register int i, val; for (i = 0; i < 256; i++) { - val = pow(i/256.0, 1.0/gamcor) * 256.0; + val = pow((i+0.5)/256.0, 1.0/gamcor) * 256.0; map[0][i] = map[1][i] = map[2][i] = val; } }