📄 psimage.c
字号:
for (lz=0;lz<nz;lz++) { lmin=FMIN(lmin,z[lz]); lmax=FMAX(lmax,z[lz]); } if (verbose==2) warn("lmin=%g lmax=%g",lmin,lmax); } if (legend) { lbeg = lmin; if (getparfloat("lbeg",&lbeg)) lbegsup=1; lend = lmax; if (getparfloat("lend",&lend)) lendsup=1; /* Change wclip,bclip to be inside legend range */ wclip = FMAX(lbeg,wclip); /* [wclip,bclip] has to be in [lbeg,lend] */ bclip = FMIN(lend,bclip); if (lbegsup!=1) { /* Add white and black areas to show possible clipping */ float rangeperc=(bclip-wclip)/20.; lbeg=wclip-rangeperc; } if (lendsup!=1) { float rangeperc=(bclip-wclip)/20.; lend=bclip+rangeperc; } lfnum = lmin; getparfloat("lfnum",&lfnum); getparstring("lstyle",&lstyles); if (STREQ("vertright",lstyles)) lstyle = VERTRIGHT; else if (STREQ("horibottom",lstyles)) lstyle = HORIBOTTOM; /* legend dimensions (BEREND), Schoenfelder */ lwidth = 0.1 ;lheight = height/2; if (lstyle==HORIBOTTOM) { lwidth=width/1.2 ;lheight = 0.24; } getparfloat("lwidth",&lwidth); getparfloat("lheight",&lheight); lx=.8;ly = ybox+(height-lheight)/2; if (lstyle==VERTRIGHT) { lx=xbox+width+0.1; } else if (lstyle==HORIBOTTOM) { lx=xbox+(width-lwidth)/2.0;ly = 1.0; } getparfloat("lx",&lx); if (lnice==1) lx = 8; /* c liner */ getparfloat("ly",&ly); getparstring("lgrid",&lgrids); if (STREQ("dot",lgrids)) ugrid = DOT; else if (STREQ("dash",lgrids)) ugrid = DASH; else if (STREQ("solid",lgrids)) ugrid = SOLID; else ugrid = NONE; } /* adjust x1beg and x1end to fall on sampled values */ /* This will not allow to display an area greater than the data supplied */ i1beg = NINT((x1beg-f1)/d1); i1beg = MAX(0,MIN(n1-1,i1beg)); x1beg = f1+i1beg*d1; i1end = NINT((x1end-f1)/d1); i1end = MAX(0,MIN(n1-1,i1end)); x1end = f1+i1end*d1; /* adjust x2beg and x2end to fall on sampled values */ i2beg = NINT((x2beg-f2)/d2); i2beg = MAX(0,MIN(n2-1,i2beg)); x2beg = f2+i2beg*d2; i2end = NINT((x2end-f2)/d2); i2end = MAX(0,MIN(n2-1,i2end)); x2end = f2+i2end*d2; if (legend) { /* Make legend color values */ int lll=0,lcount,perc5=13,ilbeg,ilend; /* color scale */ if (lbegsup!=1) { ln+=perc5; /* white area */ } if (lendsup!=1) { ln+=perc5; /* black area */ } data_legend = ealloc1(ln,sizeof(char)); if (lbegsup!=1) { for (lll=0;lll<perc5;lll++) data_legend[lll]=(char) 255; /* white area */ } for (lcount=255;lcount>=0;lcount--,lll++) data_legend[lll]=(char) lcount; if (lendsup!=1) { for (;lll<ln;lll++) data_legend[lll]=(char) 0; /* black area */ } lf=lbeg;ld=(lend-lbeg)/(ln-1); if (!(getparfloat("ldnum",&ldnum))) ldnum=0.0; /* adjust lbeg and lend to fall on sampled values */ ilbeg = NINT((lbeg-lf)/ld); ilbeg = MAX(0,MIN(ln-1,ilbeg)); lbeg = lf+ilbeg*ld; ilend = NINT((lend-lf)/ld); ilend = MAX(0,MIN(ln-1,ilend)); lend = lf+ilend*ld; } /* allocate space for image bytes */ n1c = 1+abs(i1end-i1beg); n2c = 1+abs(i2end-i2beg); cz = ealloc1(n1c*n2c,sizeof(char)); /* convert data to be imaged into unsigned characters */ zscale = (wclip!=bclip)?255.0/(wclip-bclip):1.0e10; zoffset = -bclip*zscale; i1step = (i1end>i1beg)?1:-1; i2step = (i2end>i2beg)?1:-1; 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); /* determine sampling after scaling */ n1s = MAX(1,NINT(1+(n1c-1)*d1/d1s)); d1s = (n1s>1)?d1*(n1c-1)/(n1s-1):d1; n2s = MAX(1,NINT(1+(n2c-1)*d2/d2s)); d2s = (n2s>1)?d2*(n2c-1)/(n2s-1):d2; /* if necessary, interpolate to scaled sampling intervals */ if (n1s!=n1c || n2s!=n2c) { sz = ealloc1(n1s*n2s,sizeof(char)); intl2b(n2c,d2,0.0,n1c,d1,0.0,cz,n2s,d2s,0.0,n1s,d1s,0.0,sz); /* Interpol array */ free1(cz); } else { sz = cz; } /* determine axes pads */ p1beg = (x1end>x1beg)?-fabs(d1s)/2:fabs(d1s)/2; p1end = (x1end>x1beg)?fabs(d1s)/2:-fabs(d1s)/2; p2beg = (x2end>x2beg)?-fabs(d2s)/2:fabs(d2s)/2; p2end = (x2end>x2beg)?fabs(d2s)/2:-fabs(d2s)/2; /* convert axes box parameters from inches to points */ xbox *= 72.0; ybox *= 72.0; width *= 72.0; height *= 72.0; if (legend) { lx *= 72.0; /* Schoenfelder */ ly *= 72.0; /* Schoenfelder */ lwidth *= 72.0; /* Schoenfelder */ lheight *= 72.0; /* Schoenfelder */ } /* set bounding box */ psAxesBBox( xbox,ybox,width,height, labelfont,labelsize, titlefont,titlesize, style,bbox); if (legend) { psLegendBBox( /* Space for legend Schoenfelder */ lx,ly,lwidth,lheight, labelfont,labelsize, lstyle,lbbox); /* Include space for legend Schoenfelder */ bbox[0]=MIN(bbox[0],lbbox[0]); bbox[1]=MIN(bbox[1],lbbox[1]); bbox[2]=MAX(bbox[2],lbbox[2]); bbox[3]=MAX(bbox[3],lbbox[3]); } boundingbox(bbox[0],bbox[1],bbox[2],bbox[3]); /* begin PostScript */ begineps(); /* save graphics state */ gsave(); /* translate coordinate system by box offset */ translate(xbox,ybox); /* determine image matrix */ if (style==NORMAL) { matrix[0] = 0; matrix[1] = n1s; matrix[2] = n2s; matrix[3] = 0; matrix[4] = 0; matrix[5] = 0; } else { matrix[0] = n2s; matrix[1] = 0; matrix[2] = 0; matrix[3] = -n1s; matrix[4] = 0; matrix[5] = n1s; } scale(width,height); /* draw the image (before axes so grid lines are visible) */ drawimage(hls,colors,n2s,n1s,bps,matrix,sz); /***************************/ /* main image has been drawn, restore graphics state */ grestore(); /* *********************************/ /* draw the colorbar (before axes so grid lines are visible) Schoenfelder*/ if (legend) { gsave(); translate(lx,ly); scale(lwidth,lheight); if ((lstyle==VERTLEFT) || (lstyle==VERTRIGHT)) { labmatrix[0] = 1; labmatrix[1] = 0; labmatrix[2] = 0; labmatrix[3] = ln; labmatrix[4] = 0; labmatrix[5] = 0; drawimage(hls,colors,1,ln,bps,labmatrix,data_legend); } else { labmatrix[0] = -1; labmatrix[1] = 0; labmatrix[2] = 0; labmatrix[3] = ln; labmatrix[4] = 0; labmatrix[5] = 0; rotate(-90); drawimage(hls,colors,1,ln,bps,labmatrix,data_legend); rotate(90); } grestore(); } /* draw curve */ for (i=0; i<curve; i++) { gsave(); psDrawCurve( xbox,ybox,width,height, x1beg,x1end,p1beg,p1end, x2beg,x2end,p2beg,p2end, x1curve[i],x2curve[i],npair[i], curvecolor[i],curvewidth[i],style); grestore(); } gsave(); /* draw axes and title */ psAxesBox( xbox,ybox,width,height, x1beg,x1end,p1beg,p1end, d1num,f1num,n1tic,grid1,label1, x2beg,x2end,p2beg,p2end, d2num,f2num,n2tic,grid2,label2, labelfont,labelsize, title,titlefont,titlesize, titlecolor,axescolor,gridcolor, ticwidth,axeswidth,gridwidth, style); /* restore graphics state */ grestore(); /* draw axes and title for legend Schoenfelder*/ if (legend) { float lpbeg,lpend; int lntic=1; gsave(); lpbeg = 0.0; /*(lend>lbeg)?-fabs(d1s)/2:fabs(d1s)/2;*/ lpend = 0.0; /*(lend>lbeg)?fabs(d1s)/2:-fabs(d1s)/2;*/ psLegendBox( lx,ly,lwidth,lheight, lbeg,lend,lpbeg,lpend, ldnum,lf,lntic,ugrid,units, labelfont,labelsize, axescolor,gridcolor, lstyle); grestore(); } /* end PostScript */ showpage(); endeps(); if (curve) { free1int(npair); for (i=0; i<curve; i++) { free1float(x1curve[i]); free1float(x2curve[i]); } free1float(curvewidth); free((void**)x1curve); free((void**)x2curve); free((void**)curvefile); free((void**)curvecolor); } return 0;}static void drawimage(int hls, float colors[3][3], int width, int height, int bps, float matrix[], unsigned char* z){ int row,col,rowbytes,hi,lo,zi,byte; unsigned char *rtab,*gtab,*btab,*rgb,*rgbp,*crgbp,*zp; /* handle gray scale as a special case */ if ( (hls && colors[L][0]==0.0 && colors[L][1]==0.5 && colors[L][2]==1.0 && colors[S][0]==0.0 && colors[S][1]==0.0 && colors[S][2]==0.0) || (colors[R][0]==0.0 && colors[R][1]==0.5 && colors[R][2]==1.0 && colors[G][0]==0.0 && colors[G][1]==0.5 && colors[G][2]==1.0 && colors[B][0]==0.0 && colors[B][1]==0.5 && colors[B][2]==1.0) ) { image(width,height,8,matrix,z); return; } /* allocate space for rgb tables */ rtab = ealloc1(256,sizeof(unsigned char)); gtab = ealloc1(256,sizeof(unsigned char)); btab = ealloc1(256,sizeof(unsigned char)); /* if colors are hls values, map from hls to rgb tables */ if (hls) { float hscale_low,lscale_low,sscale_low,hbase_low,lbase_low,sbase_low,h,l,s,r,g,b; float hscale_hi,lscale_hi,sscale_hi,hbase_hi,lbase_hi,sbase_hi; hbase_low = colors[H][0]; hscale_low = (colors[H][1]-colors[H][0])/127.0; lbase_low = colors[L][0]; lscale_low = (colors[L][1]-colors[L][0])/127.0; sbase_low = colors[S][0]; sscale_low = (colors[S][1]-colors[S][0])/127.0; hbase_hi = colors[H][1]; hscale_hi = (colors[H][2]-colors[H][1])/127.0; lbase_hi = colors[L][1]; lscale_hi = (colors[L][2]-colors[L][1])/127.0; sbase_hi = colors[S][1]; sscale_hi = (colors[S][2]-colors[S][1])/127.0; for (zi=0; zi<128; ++zi) { h = hbase_low+zi*hscale_low; l = lbase_low+zi*lscale_low; s = sbase_low+zi*sscale_low; hlsrgb(h*360.0,l,s,&r,&g,&b); rtab[zi] = r*255.0; gtab[zi] = g*255.0; btab[zi] = b*255.0; } for (zi=128; zi<256; ++zi) { h = hbase_hi+(zi-128)*hscale_hi; l = lbase_hi+(zi-128)*lscale_hi; s = sbase_hi+(zi-128)*sscale_hi; hlsrgb(h*360.0,l,s,&r,&g,&b); rtab[zi] = r*255.0; gtab[zi] = g*255.0; btab[zi] = b*255.0; } /* else colors are rgb values, map linearly to rgb tables */ } else { float rscale_low,gscale_low,bscale_low,rbase_low,gbase_low,bbase_low, rscale_hi,gscale_hi,bscale_hi,rbase_hi,gbase_hi,bbase_hi; rbase_low = colors[0][0]*255.0; rscale_low = 2.*(colors[0][1]-colors[0][0]); gbase_low = colors[1][0]*255.0; gscale_low = 2.*(colors[1][1]-colors[1][0]); bbase_low = colors[2][0]*255.0; bscale_low = 2.*(colors[2][1]-colors[2][0]); rbase_hi = colors[0][1]*255.0; rscale_hi = 2.*(colors[0][2]-colors[0][1]); gbase_hi = colors[1][1]*255.0; gscale_hi = 2.*(colors[1][2]-colors[1][1]); bbase_hi = colors[2][1]*255.0; bscale_hi = 2.*(colors[2][2]-colors[2][1]); for (zi=0; zi<128; ++zi) { rtab[zi] = rbase_low+zi*rscale_low; gtab[zi] = gbase_low+zi*gscale_low; btab[zi] = bbase_low+zi*bscale_low; } for (zi=128; zi<256; ++zi) { rtab[zi] = rbase_hi+(zi-128)*rscale_hi; gtab[zi] = gbase_hi+(zi-128)*gscale_hi; btab[zi] = bbase_hi+(zi-128)*bscale_hi; } } /* convert unsigned char to rgb unsigned char */ rgb = ealloc1(width*height*3,sizeof(unsigned char)); for (row=0,rgbp=rgb,zp=z; row<height; ++row) { for (col=0; col<width; ++col) { zi = *zp++; *rgbp++ = (unsigned char)rtab[zi]; *rgbp++ = (unsigned char)gtab[zi]; *rgbp++ = (unsigned char)btab[zi]; } } /* free tables */ free1(rtab); free1(gtab); free1(btab); /* if necessary, compress 24 bit color to 12 bit color */ if (bps==12) { rowbytes = 1+(width*12-1)/8; crgbp = rgbp = rgb; for (row=0; row<height; ++row) { for (byte=0; byte<rowbytes-1; ++byte) { hi = (*rgbp++)>>4; lo = (*rgbp++)>>4; *crgbp++ = (hi<<4)|lo; } if (width%2) { hi = (*rgbp++)>>4; *crgbp++ = hi<<4; } else { hi = (*rgbp++)>>4; lo = (*rgbp++)>>4; *crgbp++ = (hi<<4)|lo; } } } /* draw color image */ rgbimage(width,height,bps/3,matrix,rgb); /* free workspace */ free1(rgb);}/* internal functions to convert HLS to RGB (adapted from Foley & Van Dam) */static float rgbvalue (float n1, float n2, float hue){ while (hue>360.0) hue -= 360.0; while (hue<0.0) hue += 360.0; if (hue<60.0) return n1+(n2-n1)*hue/60.0; else if (hue<180.0) return n2; else if (hue<240.0) return n1+(n2-n1)*(240.0-hue)/60.0; else return n1;}static void hlsrgb (float h, float l, float s, float *r, float *g, float *b){ float m1,m2; if (l<=0.5) m2 = l*(1.0+s); else m2 = l+s-l*s; m1 = 2*l-m2; if (s==0.0) { *r = *g = *b = l; } else { *r = rgbvalue(m1,m2,h+120.0); *g = rgbvalue(m1,m2,h); *b = rgbvalue(m1,m2,h-120.0); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -