⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ximage.c

📁 地震波正演和显示模块
💻 C
📖 第 1 页 / 共 4 页
字号:
compute a readable scale for use in plotting axes
******************************************************************************
Input:
x1		first x value
x2		second x value
nxnum		desired number of numbered values

Output:
nxnum		number of numbered values
dxnum		increment between numbered values (dxnum>0.0)
fxnum		first numbered value
******************************************************************************
Notes:
scaxis attempts to honor the user-specified nxnum.  However, nxnum
will be modified if necessary for readability.  Also, fxnum and nxnum
will be adjusted to compensate for roundoff error; in particular, 
fxnum will not be less than xmin-eps, and fxnum+(nxnum-1)*dxnum 
will not be greater than xmax+eps, where eps = 0.0001*(xmax-xmin).
xmin is the minimum of x1 and x2.  xmax is the maximum of x1 and x2.
*****************************************************************************/
{
	int n,i,iloga;
	float d,f,rdint[4],eps,a,b,xmin,xmax;

	/* set readable intervals */
	rdint[0] = 1.0;  rdint[1] = 2.0;  rdint[2] = 5.0;  rdint[3] = 10.0;

	/* handle x1==x2 as a special case */
	if  (x1==x2) {
		*nxnum = 1;
		*dxnum = 1.0;
		*fxnum = x1;
		return;
	}

	/* determine minimum and maximum x */
	xmin = (x1<x2)?x1:x2;
	xmax = (x1>x2)?x1:x2;
	
	/* get desired number of numbered values */
	n = *nxnum;
	n = (2>n)?2:n;
	
	/* determine output parameters, adjusted for roundoff */
	a = (xmax-xmin)/(float)(n-1);
	iloga = (int)log10(a);
	if (a<1.0) iloga = iloga - 1;
	b = a/pow(10.0,(double)iloga);
	for (i=0; i<3 && b>=sqrt(rdint[i]*rdint[i+1]); i++);
	d = rdint[i]*pow(10.0,(float)iloga);
	f = ((int)(xmin/d))*d-d;
	eps = 0.0001*(xmax-xmin);
	while(f<(xmin-eps))
		 f = f+d;
	n = 1+(int)((xmax+eps-f)/d); 
        
	/* set output parameters before returning */
	*nxnum = n;
	*dxnum = d;
	*fxnum = f;
}

void strchop(char *s, char *t)
/***********************************************************************
strchop - chop off the tail end of a string "s" after a "," returning
	  the front part of "s" as "t".
************************************************************************
Notes:
Based on strcpy in Kernighan and Ritchie's C [ANSI C] book, p. 106.
*/
{

	while ( (*s != ',') && (*s != '\0') ) {
		 *t++ = *s++;
	}
	*t='\0';
}


void warn(char *fmt, ...)
{
	va_list args;

	if (EOF == fflush(stdout)) {
		fprintf(stderr, "\nwarn: fflush failed on stdout");
	}
	fprintf(stderr, "\n%s: ", xargv[0]);
	va_start(args,fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
	fprintf(stderr, "\n");
	return;
}


/*********************** self documentation **********************/
/*****************************************************************************
INTL2B - bilinear interpolation of a 2-D array of bytes

intl2b		bilinear interpolation of a 2-D array of bytes

******************************************************************************
Function Prototype:
void intl2b (int nxin, float dxin, float fxin,
	int nyin, float dyin, float fyin, unsigned char *zin,
	int nxout, float dxout, float fxout,
	int nyout, float dyout, float fyout, unsigned char *zout);

******************************************************************************
Input:
nxin		number of x samples input (fast dimension of zin)
dxin		x sampling interval input
fxin		first x sample input
nyin		number of y samples input (slow dimension of zin)
dyin		y sampling interval input
fyin		first y sample input
zin		array[nyin][nxin] of input samples (see notes)
nxout		number of x samples output (fast dimension of zout)
dxout		x sampling interval output
fxout		first x sample output
nyout		number of y samples output (slow dimension of zout)
dyout		y sampling interval output
fyout		first y sample output

Output:
zout		array[nyout][nxout] of output samples (see notes)

******************************************************************************
Notes:
The arrays zin and zout must passed as pointers to the first element of
a two-dimensional contiguous array of unsigned char values.

Constant extrapolation of zin is used to compute zout for
output x and y outside the range of input x and y.
 
For efficiency, this function builds a table of interpolation
coefficents pre-multiplied by byte values.  To keep the table
reasonably small, the interpolation does not distinguish
between x and y values that differ by less than dxin/ICMAX
and dyin/ICMAX, respectively, where ICMAX is a parameter
defined above.

*****************************************************************************/
/**************** end self doc ********************************/


/* number of tabulated interpolation coefficients */
#define ICMAX 99 /* must be odd, so that ICMAC-ic!=ic, for ic=0 to ICMAX/2! */
#define NTABLE (ICMAX+1)

/* functions defined and used internally */
static void intl2bx(int, int*, int*, int,
	unsigned char[][256], unsigned char*, unsigned char*);
static void intl2by(int, int, int, unsigned char[][256],
	unsigned char*, unsigned char*, unsigned char*);

void intl2b(int nxin, float dxin, float fxin,
	int nyin, float dyin, float fyin, unsigned char *zin,
	int nxout, float dxout, float fxout,
	int nyout, float dyout, float fyout, unsigned char *zout)
/*****************************************************************************
bilinear interpolation of a 2-D array of bytes
******************************************************************************
Input:
nxin		number of x samples input (fast dimension of zin)
dxin		x sampling interval input
fxin		first x sample input
nyin		number of y samples input (slow dimension of zin)
dyin		y sampling interval input
fyin		first y sample input
zin		array[nyin][nxin] of input samples (see notes)
nxout		number of x samples output (fast dimension of zout)
dxout		x sampling interval output
fxout		first x sample output
nyout		number of y samples output (slow dimension of zout)
dyout		y sampling interval output
fyout		first y sample output

Output:
zout		array[nyout][nxout] of output samples (see notes)
******************************************************************************
Notes:
The arrays zin and zout must passed as pointers to the first element of
a two-dimensional contiguous array of unsigned char values.

Constant extrapolation of zin is used to compute zout for
output x and y outside the range of input x and y.
 
For efficiency, this function builds a table of interpolation
coefficents pre-multiplied by byte values.  To keep the table
reasonably small, the interpolation does not distinguish
between x and y values that differ by less than dxin/ICMAX
and dyin/ICMAX, respectively, where ICMAX is a parameter
defined above.

*****************************************************************************/
{         
	int ixout,iyout,ic,ib,iyin,iyinl=1;
	float xout,yout,rxin,ryin,frac;
	int *kzin,*kic;
	unsigned char *temp1,*temp2,*temp;
	static unsigned char table[NTABLE][256];
	static int tabled=0;

	/* if not already built, build byte multiplication table */
	if (!tabled) {
		for (ic=0; ic<=ICMAX/2; ++ic) {
			frac = (float)(ic)/(float)ICMAX;
			for (ib=0; ib<256; ++ib) {
				table[ic][ib] = frac*ib;
				table[ICMAX-ic][ib] = ib-table[ic][ib];
			}
		}
		tabled = 1;
	}                                              

	/* get workspace */
	kzin = (int*)malloc(nxout*sizeof(int));
	kic = (int*)malloc(nxout*sizeof(int));
	temp1 = (unsigned char*)malloc(nxout*sizeof(unsigned char));
	temp2 = (unsigned char*)malloc(nxout*sizeof(unsigned char));

	/* pre-compute indices for fast 1-D interpolation along x axis */
	for (ixout=0,xout=fxout; ixout<nxout; ixout++,xout+=dxout) {
		rxin = (xout-fxin)/dxin;
		if (rxin<=0) {
		 	kzin[ixout] = 0;
		 	kic[ixout] = 0;
		} else if (rxin>=nxin-1) {
		 	kzin[ixout] = nxin-2;
		 	kic[ixout] = ICMAX*256;
		} else {
		 	kzin[ixout] = (int)rxin;
		 	frac = rxin-(int)rxin;
			ic = frac*ICMAX+0.5;
		 	kic[ixout] = ic*256;
		}
	}

	/* loop over output y */
	for (iyout=0,yout=fyout; iyout<nyout; iyout++,yout+=dyout) {

		/* compute index of input y, clipped to range of input y */
		ryin = MAX(0,MIN(nyin-1,(yout-fyin)/dyin));
		iyin = MAX(0,MIN(nyin-2,ryin));

		/* if output y is not between current input y */
		if (iyin!=iyinl || iyout==0) {

			/* if 2nd temporary vector is still useful */
			if (iyin==iyinl+1 && iyout!=0) {              

				/* swap 2nd and 1st temp; compute 2nd temp */
				temp = temp1;
				temp1 = temp2;
				temp2 = temp;
				intl2bx(nxout,kzin,kic,ICMAX,
					table,zin+(iyin+1)*nxin,temp2);

			/* else if 1st temporary vector is still useful */
			} else if (iyin==iyinl-1 && iyout!=0) {

				/* swap 1st and 2nd temp; compute 1st temp */
				temp = temp1;
				temp1 = temp2;
				temp2 = temp;
				intl2bx(nxout,kzin,kic,ICMAX,
					table,zin+iyin*nxin,temp1);

			/* else if neither 1st or 2nd temp is useful */
			} else {

				/* compute 1st and 2nd temporary vectors */
				intl2bx(nxout,kzin,kic,ICMAX,
					table,zin+iyin*nxin,temp1);
				intl2bx(nxout,kzin,kic,ICMAX,
					table,zin+(iyin+1)*nxin,temp2);
			}                 

			/* remember last index of input y */
			iyinl = iyin;
		}

		/* compute index of interpolation coefficient */
		frac = ryin-iyin;
		ic = frac*ICMAX+0.5;

		/* linearly interpolate output vector by table lookup */
		intl2by(nxout,ic,ICMAX,table,
  			temp1,temp2,zout+iyout*nxout);
	}                         

	/* free workspace before returning */
	free(kzin);
	free(kic);                     
	free(temp1);
	free(temp2);
}
   
static void intl2bx(int nxout, int *kzin, int *kic, int icmax, 
	unsigned char table[][256], unsigned char *zin, unsigned char *zout)
/****************************************************************************
 interpolate between input x values (FOR INTERNAL USE by intl2b) 
****************************************************************************/
{
	int ixout,jzin,jic;
  	unsigned char *tablel,*tableh;
	tablel = &table[0][0];
	tableh = &table[icmax][0];
	for (ixout=0; ixout<nxout; ixout++) {   
		jzin = kzin[ixout];
		jic = kic[ixout];
	 	zout[ixout] = tableh[(int)zin[jzin]-jic] 
				+ tablel[(int)zin[jzin+1]+jic];
	}
}   

static void intl2by(int nxout, int ic, int icmax, unsigned char table[][256],
	unsigned char *temp1, unsigned char *temp2, unsigned char *zout)
/****************************************************************************
 interpolate between input y values (FOR INTERNAL USE by intl2b) 
****************************************************************************/
{
	int ixout;
	unsigned char *tablel, *tableh;
	tablel = &table[ic][0];
	tableh = &table[icmax-ic][0];
	for (ixout=0; ixout<nxout; ixout++)
		zout[ixout] = tableh[temp1[ixout]]+tablel[temp2[ixout]];
}

/*********************** self documentation **********************/
/*****************************************************************************
WINDOW - Function to create a window in X-windows graphics

xNewWindow	Create a new window and return the window ID

******************************************************************************
Function Prototype:
Window xNewWindow (Display *dpy, int x, int y, int width, int height,
	int border, int background, char *name);

******************************************************************************
Input:
dpy		display pointer
x		x in pixels of upper left corner
y		y in pixels of upper left corner
width		width in pixels
height		height in pixels
border		border pixel
background	background pixel
name		name of window (also used for icon)

******************************************************************************
Notes:
The parent window is the root window.
The border_width is 4 pixels.
*****************************************************************************/
/**************** end self doc ********************************/



Window
xNewWindow (Display *dpy, int x, int y, int width, int height,
	int border, int background, char *name)
/*****************************************************************************
Create a new window and return the window ID
******************************************************************************
Input:
dpy		display pointer
x		x in pixels of upper left corner
y		y in pixels of upper left corner
width		width in pixels
height		height in pixels
border		border pixel
background	background pixel
name		name of window (also used for icon)
******************************************************************************
Notes:
The parent window is the root window.
The border_width is 4 pixels.
*****************************************************************************/
{
	Window root,win;
	XSizeHints size_hints;
	XWMHints wm_hints;
	int scr,border_w=4;

	/* get screen and root window */
	scr = DefaultScreen(dpy);
	root = RootWindow(dpy,scr);

	/* create window */
	win = XCreateSimpleWindow(dpy,root,x,y,width,height,
		border_w,border,background);

	/* set window properties for window manager */
	size_hints.flags = USPosition|USSize;
	size_hints.x = x;
	size_hints.y = y;
	size_hints.width = width;
	size_hints.height = height;
	XSetStandardProperties(dpy,win,name,name,None,0,0,&size_hints);
	wm_hints.flags = InputHint;
	wm_hints.input = True;
	XSetWMHints(dpy,win,&wm_hints);

	/* return window ID */
	return win;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -