📄 ximage.c
字号:
/* allocate space for image bytes */
n1c = 1+abs(i1end-i1beg);
n2c = 1+abs(i2end-i2beg);
cz = alloc1(n1c*n2c,sizeof(unsigned char));
/* convert data to be imaged into signed characters */
zscale = (wclip!=bclip)?255.0/(wclip-bclip):1.0e10;
zoffset = -bclip*zscale;
i1step = (i1end>i1beg)?1:-1;
i2step = (i2end>i2beg)?1:-1;
if (style==NORMAL) {
for (i2c=0,i2=i2beg; i2c<n2c; i2c++,i2+=i2step) {
czp = cz+n1c*n2c-(i2c+1)*n1c;
for (i1c=0,i1=i1beg; i1c<n1c; i1c++,i1+=i1step) {
zi = zoffset+z[i1+i2*n1]*zscale;
if (zi<0.0) zi = 0.0;
if (zi>255.0) zi = 255.0;
*czp++ = (unsigned char)zi;
}
}
} else {
czp = cz;
for (i1c=0,i1=i1beg; i1c<n1c; i1c++,i1+=i1step) {
for (i2c=0,i2=i2beg; i2c<n2c; i2c++,i2+=i2step) {
zi = zoffset+z[i1+i2*n1]*zscale;
if (zi<0.0) zi = 0.0;
if (zi>255.0) zi = 255.0;
*czp++ = (unsigned char)zi;
}
}
}
free1float(z);
/* initialize zoom box parameters */
nxb = nx = (style==NORMAL ? n1c : n2c);
nyb = ny = (style==NORMAL ? n2c : n1c);
ixb = iyb = 0;
czb = cz;
x1begb = x1beg; x1endb = x1end;
x2begb = x2beg; x2endb = x2end;
/* connect to X server */
if ((dpy=XOpenDisplay(NULL))==NULL)
warn("Cannot connect to display %s!\n",XDisplayName(NULL));
scr = DefaultScreen(dpy);
black = BlackPixel(dpy,scr);
white = WhitePixel(dpy,scr);
/* create window */
win = xNewWindow(dpy,xbox,ybox,wbox,hbox,(int) black,(int) white,windowtitle);
/* backwards compatibility */
if (STREQ(cmap,"gray")) {
sprintf(cmap,"%s","rgb0");
} else if (STREQ(cmap,"hue")) {
/* free1(cmap); */
cmap = (char *)alloc1(5,1);
sprintf(cmap,"%s","hsv1");
} else if ((strncmp(cmap,"hsv",3)) && (strncmp(cmap,"rgb",3))){
if (verbose) warn ("cmap=%s using cmap=gray", cmap);
/* free1(cmap); */
cmap = (char *)alloc1(5,1);
sprintf (cmap, "%s", "rgb0");
}
/* here are the new colormaps */
if (strncmp(cmap, "rgb", 3) == 0)
XSetWindowColormap(dpy,win,
xCreateRGBColormap(dpy,win, cmap, verbose));
else if (strncmp (cmap, "hsv", 3) == 0)
XSetWindowColormap(dpy,win,
xCreateHSVColormap(dpy,win, cmap, verbose));
/* determine min and max pixels from standard colormap */
pmin = xGetFirstPixel(dpy);
pmax = xGetLastPixel(dpy);
if (verbose) warn("pmin=%x,pmax=%x\n",pmin,pmax);
if(pmax==0L)pmax=255L;
if (verbose) warn("pmin=%x,pmax=%x\n",pmin,pmax);
data_legend = (unsigned char *) malloc(lwidth * lheight);
for (i=0; i<lwidth*lheight; i++){
data_legend[i] = (unsigned char) (0 + (255+1)*(i/lwidth*1.0)/(1.0*lheight));
/* fprintf(stderr," %d ",data_legend[i]);*/
}
/* make GC for image */
gci = XCreateGC(dpy,win,0,NULL);
/* set normal event mask */
XSelectInput(dpy,win,
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(imageOutOfDate|(~imageOutOfDate)/*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) {
czbi = newInterpBytes(nxb,nyb,czb,
width,height,blockinterp);
if (image!=NULL) XDestroyImage(image);
image = xNewImage(dpy,pmin,pmax,
width,height,blank,czbi);
/* BEREND create image */
if (legend) {
if (image_legend!=NULL) XDestroyImage(image_legend);
image_legend = xNewImage(dpy,pmin,pmax,lwidth,lheight,0,data_legend);
}
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 curve on top of image */
for (i=0; i<curve; i++)
xDrawCurve(dpy,win,
x,y,width,height,
x1begb,x1endb,0.0,0.0,
x2begb,x2endb,0.0,0.0,
x1curve[i],x2curve[i],npair[i],
curvecolor[i],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;
fseeko(infp,(off_t)0,SEEK_SET);
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) {
Colormap mycp=xCreateRGBColormap(dpy,win,"rgb_up",verbose);
XSetWindowColormap(dpy,win,mycp);
XInstallColormap(dpy,mycp);
/* 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 (keysym==XK_R) {
Colormap mycp=xCreateRGBColormap(dpy,win,"rgb_down",verbose);
XSetWindowColormap(dpy,win,mycp);
XInstallColormap(dpy,mycp);
/* 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 (keysym==XK_h) {
Colormap mycp=xCreateHSVColormap(dpy,win,"hsv_up",verbose);
XSetWindowColormap(dpy,win,mycp);
XInstallColormap(dpy,mycp);
/* 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 (keysym==XK_H) {
Colormap mycp=xCreateHSVColormap(dpy,win,"hsv_down",verbose);
XSetWindowColormap(dpy,win,mycp);
XInstallColormap(dpy,mycp);
/* 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 {
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 = alloc1(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);
if (curve) {
free1int(npair);
for (i=0; i<curve; i++) {
free1float(x1curve[i]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -