📄 ximage.c
字号:
StructureNotifyMask | ExposureMask | KeyPressMask | PointerMotionMask | ButtonPressMask | ButtonReleaseMask | Button1MotionMask | Button2MotionMask); /* map window */ XMapWindow(dpy,win); /* determine good size for axes box */ xSizeAxesBox(dpy,win, labelfont,titlefont,style, &x,&y,&width,&height); /* clear the window */ XClearWindow(dpy,win); /* note that image is out of date */ imageOutOfDate = 1; /* main event loop */ while(True) { XNextEvent(dpy,&event); /* if window was resized */ if (event.type==ConfigureNotify && (event.xconfigure.width!=winwidth || event.xconfigure.height!=winheight)) { winwidth = event.xconfigure.width; winheight = event.xconfigure.height; /* determine good size for axes box */ xSizeAxesBox(dpy,win, labelfont,titlefont,style, &x,&y,&width,&height); /* clear the window */ XClearWindow(dpy,win); /* note that image is out of date */ imageOutOfDate = 1; /* else if window exposed */ } else if (event.type==Expose) { /* clear all expose events from queue */ while (XCheckTypedEvent(dpy,Expose,&event)); /* if necessary, make new image */ if (imageOutOfDate) { if (czbi!=NULL) free1(czbi); czbi = newInterpBytes(nxb,nyb,czb, width,height); if (image!=NULL) XDestroyImage(image); image = xNewImage(dpy,pmin,pmax, width,height,blank,czbi); imageOutOfDate = 0; } /* draw image (before axes so grid lines visible) */ XPutImage(dpy,win,gci,image,0,0,x,y, image->width,image->height); /* BEREND display image */ if (legend) XPutImage(dpy,win,gci,image_legend, 0,0,lx,y+ly,lwidth,lheight); /* BEREND draw legend axes on top of image */ if (legend) xDrawLegendBox(dpy,win, lx,y+ly,lwidth,lheight, bclip,wclip,units,legendfont, labelfont,title,titlefont, labelcolor,titlecolor,gridcolor, style); /* draw axes on top of image */ xDrawAxesBox(dpy,win, x,y,width,height, x1begb,x1endb,0.0,0.0, d1num,f1num,n1tic,grid1,label1, x2begb,x2endb,0.0,0.0, d2num,f2num,n2tic,grid2,label2, labelfont,title,titlefont, labelcolor,titlecolor,gridcolor, style); /* else if key down */ } else if (event.type==KeyPress) { XLookupString(&(event.xkey),keybuf,0,&keysym,&keystat); if (keysym==XK_s) { xMousePrint(event,style, mpicksfp, x,y,width,height, x1begb,x1endb,x2begb,x2endb); } else if (keysym==XK_q || keysym==XK_Q) { /* This is the exit from the event loop */ break; } else if (keysym==XK_p || keysym==XK_P) { /* invoke pswigb with appropriate data */ char cmdline[1024], cmdtemp[256]; float cmdfloat; int iargc; efseek(infp,0L,0); strcpy(cmdline,"psimage"); for(iargc = 1; iargc < argc; iargc++) { strcat(cmdline," "); strcat(cmdline,argv[iargc]); } /* override incompatible arguments */ sprintf(cmdtemp," axescolor=%s",labelcolor); strcat(cmdline,cmdtemp); cmdfloat = DisplayWidthMM(dpy,scr)/25.4; cmdfloat /= DisplayWidth(dpy,scr); sprintf(cmdtemp," wbox=%g", cmdfloat*width); strcat(cmdline,cmdtemp); sprintf(cmdtemp," xbox=%g", 0.5+cmdfloat*xbox); strcat(cmdline,cmdtemp); cmdfloat = DisplayHeightMM(dpy,scr)/25.4; cmdfloat /= DisplayHeight(dpy,scr); sprintf(cmdtemp," hbox=%g", cmdfloat*height); strcat(cmdline,cmdtemp); sprintf(cmdtemp," ybox=%g", 0.5+cmdfloat*ybox); strcat(cmdline,cmdtemp); sprintf(cmdtemp," x1beg=%g", x1begb); strcat(cmdline,cmdtemp); sprintf(cmdtemp," x1end=%g",x1endb); strcat(cmdline,cmdtemp); sprintf(cmdtemp," x2beg=%g", x2begb); strcat(cmdline,cmdtemp); sprintf(cmdtemp," x2end=%g",x2endb); strcat(cmdline,cmdtemp); if (STREQ(cmap,"gray")) { strcat(cmdline," brgb=0.0,0.0,0.0"); strcat(cmdline," wrgb=1.0,1.0,1.0"); } else if (STREQ(cmap,"hue")) { strcat(cmdline," bhls=0.75,0.5,1.0"); strcat(cmdline," whls=0.0,0.5,1.0"); } strcat(cmdline," title=\""); strcat(cmdline,title); strcat(cmdline,"\""); strcat(cmdline," label1=\""); strcat(cmdline,label1); strcat(cmdline,"\""); strcat(cmdline," label2=\""); strcat(cmdline,label2); strcat(cmdline,"\""); fprintf(stderr,"%s\n",cmdline); system(cmdline); } else if (keysym==XK_r) { XSetWindowColormap(dpy,win, xCreateRGBColormap(dpy,win,"rgb_up", verbose)); } else if (keysym==XK_R) { XSetWindowColormap(dpy,win, xCreateRGBColormap(dpy,win,"rgb_down", verbose)); } else if (keysym==XK_h) { XSetWindowColormap(dpy,win, xCreateHSVColormap(dpy,win,"hsv_up", verbose)); } else if (keysym==XK_H) { XSetWindowColormap(dpy,win, xCreateHSVColormap(dpy,win,"hsv_down", verbose)); } else { continue; } /* else if button down (1 == zoom, 2 == mouse tracking */ } else if (event.type==ButtonPress) { /* if 1st button: zoom */ if (event.xbutton.button==Button1) { /* track pointer and get new box */ xRubberBox(dpy,win,event,&xb,&yb,&wb,&hb); /* if new box has tiny width or height */ if (wb<4 || hb<4) { /* reset box to initial values */ x1begb = x1beg; x1endb = x1end; x2begb = x2beg; x2endb = x2end; nxb = nx; nyb = ny; ixb = iyb = 0; if (czb!=cz) free1(czb); czb = cz; /* else, if new box has non-zero width */ /* and height */ } else { /* calculate new box parameters */ if (style==NORMAL) { zoomBox(x,y,width,height, xb,yb,wb,hb, nxb,ixb,x1begb,x1endb, nyb,iyb,x2endb,x2begb, &nxb,&ixb,&x1begb,&x1endb, &nyb,&iyb,&x2endb,&x2begb); } else { zoomBox(x,y,width,height, xb,yb,wb,hb, nxb,ixb,x2begb,x2endb, nyb,iyb,x1begb,x1endb, &nxb,&ixb,&x2begb,&x2endb, &nyb,&iyb,&x1begb,&x1endb); } /* make new bytes in zoombox */ if (czb!=cz) free1(czb); czb = ealloc1(nxb*nyb, sizeof(signed char)); for (i=0,czbp=czb; i<nyb; i++) { czp = cz+(iyb+i)*nx+ixb; for (j=0; j<nxb; j++) *czbp++ = *czp++; } } /* clear area and force an expose event */ XClearArea(dpy,win,0,0,0,0,True); /* note that image is out of date */ imageOutOfDate = 1; /* else if 2nd button down: display mouse coords */ } else if (event.xbutton.button==Button2) { showloc = 1; xMouseLoc(dpy,win,event,style,showloc, x,y,width,height,x1begb,x1endb, x2begb,x2endb); } else { continue; } /* else if pointer has moved */ } else if (event.type==MotionNotify) { /* if button2 down, show mouse location */ if (showloc) xMouseLoc(dpy,win,event,style,True, x,y,width,height, x1begb,x1endb,x2begb,x2endb); /* else if button2 released, stop tracking */ } else if (event.type==ButtonRelease && event.xbutton.button==Button2) { showloc = 0; } } /* end of event loop */ /* close connection to X server */ XCloseDisplay(dpy); return EXIT_SUCCESS;}/* update parameters associated with zoom box */static void zoomBox (int x, int y, int w, int h, int xb, int yb, int wb, int hb, int nx, int ix, float x1, float x2, int ny, int iy, float y1, float y2, int *nxb, int *ixb, float *x1b, float *x2b, int *nyb, int *iyb, float *y1b, float *y2b){ /* if width and/or height of box are zero, just copy values */ if (wb==0 || hb==0) { *nxb = nx; *ixb = ix; *x1b = x1; *x2b = x2; *nyb = ny; *iyb = iy; *y1b = y1; *y2b = y2; return; } /* clip box */ if (xb<x) { wb -= x-xb; xb = x; } if (yb<y) { hb -= y-yb; yb = y; } if (xb+wb>x+w) wb = x-xb+w; if (yb+hb>y+h) hb = y-yb+h; /* determine number of samples in rubber box (at least 2) */ *nxb = MAX(nx*wb/w,2); *nyb = MAX(ny*hb/h,2); /* determine indices of first samples in box */ *ixb = ix+(xb-x)*(nx-1)/w; *ixb = MIN(*ixb,ix+nx-*nxb); *iyb = iy+(yb-y)*(ny-1)/h; *iyb = MIN(*iyb,iy+ny-*nyb); /* determine box limits to nearest samples */ *x1b = x1+(*ixb-ix)*(x2-x1)/(nx-1); *x2b = x1+(*ixb+*nxb-1-ix)*(x2-x1)/(nx-1); *y1b = y1+(*iyb-iy)*(y2-y1)/(ny-1); *y2b = y1+(*iyb+*nyb-1-iy)*(y2-y1)/(ny-1);}/* return pointer to new interpolated array of bytes */static unsigned char *newInterpBytes (int n1in, int n2in, unsigned char *bin, int n1out, int n2out){ unsigned char *bout; float d1in,d2in,d1out,d2out,f1in,f2in,f1out,f2out; f1in = f2in = f1out = f2out = 0.0; d1in = d2in = 1.0; d1out = d1in*(float)(n1in-1)/(float)(n1out-1); d2out = d2in*(float)(n2in-1)/(float)(n2out-1); bout = ealloc1(n1out*n2out,sizeof(unsigned char)); intl2b(n1in,d1in,f1in,n2in,d2in,f2in,bin, n1out,d1out,f1out,n2out,d2out,f2out,bout); return bout;} void xMouseLoc(Display *dpy, Window win, XEvent event, int style, Bool show, int x, int y, int width, int height, float x1begb, float x1endb, float x2begb, float x2endb){ static XFontStruct *fs=NULL; static XCharStruct overall; static GC gc; int dummy,xoffset=5,yoffset=5; float x1,x2; char string[256]; /* if first time, get font attributes and make gc */ if (fs==NULL) { fs = XLoadQueryFont(dpy,"fixed"); gc = XCreateGC(dpy,win,0,NULL); /* make sure foreground/background are black/white */ XSetForeground(dpy,gc,BlackPixel(dpy,DefaultScreen(dpy))); XSetBackground(dpy,gc,WhitePixel(dpy,DefaultScreen(dpy))); XSetFont(dpy,gc,fs->fid); overall.width = 1; overall.ascent = 1; overall.descent = 1; } /* erase previous string */ XClearArea(dpy,win,xoffset,yoffset, overall.width,overall.ascent+overall.descent,False); /* if not showing, then return */ if (!show) return; /* convert mouse location to (x1,x2) coordinates */ if (style==NORMAL) { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.x-x)/width; x2 = x2endb+(x2begb-x2endb)*(event.xmotion.y-y)/height; } else { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.y-y)/height; x2 = x2begb+(x2endb-x2begb)*(event.xmotion.x-x)/width; } /* draw string indicating mouse location */ sprintf(string,"(%0.6g,%0.6g)",x1,x2); XTextExtents(fs,string,strlen(string),&dummy,&dummy,&dummy,&overall); XDrawString(dpy,win,gc,xoffset,yoffset+overall.ascent, string,strlen(string));}void xMousePrint(XEvent event, int style, FILE *mpicksfp, int x, int y, int width, int height, float x1begb, float x1endb, float x2begb, float x2endb){ float x1,x2; /* convert mouse location to (x1,x2) coordinates */ if (style==NORMAL) { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.x-x)/width; x2 = x2endb+(x2begb-x2endb)*(event.xmotion.y-y)/height; } else { x1 = x1begb+(x1endb-x1begb)*(event.xmotion.y-y)/height; x2 = x2begb+(x2endb-x2begb)*(event.xmotion.x-x)/width; } /* write string indicating mouse location */ fprintf(mpicksfp, "%0.6g %0.6g\n", x1, x2);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -