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

📄 utility.c.bak

📁 学习跟踪的好程序
💻 BAK
字号:
/*****************************************************************************
 * File:	utiltiy.c
 * Desc:	useful utility fuctions
 * Author:	Xuhui Zhou  @ Carnegie Mellon University
 * Date:	Sep 22, 2003
 *****************************************************************************/
#include "utility.h"
#include "klt/klt.h"

/*****************************************************************************
 * Name: fn_Round
 *****************************************************************************/
double fn_Round(double input){
	double output, remain;

	output = floor(input);
	remain = input - output;
	if (remain>=0.5) output++;
	
	return((int)output);
}

/* find kth smallest of n double array.
 * N.B. array is rearranged IN PLACE.
 */
double fn_kthSmallest(double a[], int n, int k)
{
    int i,j,l,m ;
    double t, x ;

    l=0 ; m=n-1 ;
    while (l<m) {
        x=a[k] ;
        i=l ;
        j=m ;
        do {
            while (a[i]<x) i++ ;
            while (x<a[j]) j-- ;
            if (i<=j) {
		t=a[i]; a[i]=a[j]; a[j]=t;
                i++ ; j-- ;
            }
        } while (i<=j) ;
        if (j<k) l=i ;
        if (k<i) m=j ;
    }
    return a[k] ;
}


/* find median of n double array.
 * N.B. array is rearranged IN PLACE.
 */
double fn_median(double a[], int n)
{
	return (fn_kthSmallest (a, n, n/2));
}


/*****************************************************************************
 * Name: fn_Abs                                                           *
 *****************************************************************************/
double fn_Abs(double input){
	if (input<0)
		input = - input;
	
	return(input);
}

/*****************************************************************************
 * Name: fn_DoubleToStr                                                           *
 *****************************************************************************/
char* fn_DoubleToStr(double input, int precision){
	int length;
	char *strTemp; 
	//char strOutput[100];
	int decimal, sign, i;
	
	strTemp = fcvt(input, precision, &decimal, &sign);
	length = strlen(strTemp);
	if (decimal == 0){
		for(i=length;i>=0;i--){
			strTemp[i+2] = strTemp[i];
		}
		strTemp[1] = '.';
		strTemp[0] = '0';
	}
	if (decimal == -1){
		for(i=length;i>=0;i--){
			strTemp[i+3] = strTemp[i];
		}
		strTemp[2] = '0';
		strTemp[1] = '.';
		strTemp[0] = '0';
	}
	if (decimal >0)
	{
		for(i=length;i>=decimal;i--){
			strTemp[i+1] = strTemp[i];
		}
		strTemp[decimal] = '.';
	}

	length = strlen(strTemp);
	if (sign==1){ //negative
		for(i=length; i>=0; i--){
			strTemp[i+1] = strTemp[i];
		}
		strTemp[0] = '-';
	}	

//	strcpy(strOutput, strTemp);
//	return (strOutput);

	return(strTemp);
}

/*****************************************************************************
 * Name: fn_DoubleToStr2                                                           *
 *****************************************************************************/
void fn_DoubleToStr2(double input, int precision, char* strOutput)
{
	int length;
	char *strTemp; 
	int decimal, sign, i;	
		
	strTemp = fcvt(input, precision, &decimal, &sign);
	length = strlen(strTemp);
	if (decimal == 0){
		for(i=length;i>=0;i--){
			strTemp[i+2] = strTemp[i];
		}
		strTemp[1] = '.';
		strTemp[0] = '0';
	}
	else if (decimal <0){
		decimal = -decimal;
		//shift string for 2+decimal times. 2 stands for '0.' in '0.xxx'
		for(i=length;i>=0;i--){
			strTemp[i+2+decimal] = strTemp[i];
		}
		//pad 0 for |decimal| times
		for(i=2;i<2+decimal;i++){		
			strTemp[i] = '0';
		}		
		strTemp[1] = '.';
		strTemp[0] = '0';
	}
	else //(decimal >0)
	{
		//shift one position for '.' in 'xxx.xxx'
		for(i=length;i>=decimal;i--){
			strTemp[i+1] = strTemp[i];
		}
		strTemp[decimal] = '.';
	}

	length = strlen(strTemp);
	if (sign==1){ //negative
		for(i=length; i>=0; i--){
			strTemp[i+1] = strTemp[i];
		}
		strTemp[0] = '-';
	}	

	strcpy(strOutput, strTemp);
//	return (strOutput);

//	return(strTemp);
}

///////////////////////////////////////////////////////////////////////////////
void utl_RectZoom(RECT *ioRect, float inZoomFactor, int imgWidth, int imgHeight)
//scale a rect by a zoom factor
{
	int width;
	int height;
	int halfDx;
	int halfDy;

	if (inZoomFactor==1) return;

	//get rect dimension
	width = ioRect->right - ioRect->left;
	height = ioRect->bottom - ioRect->top;

	//get zoom difference
	halfDx = (int)fn_Round(width*(inZoomFactor-1)/2);
	halfDy = (int)fn_Round(height*(inZoomFactor-1)/2);

	//get out Rect
	ioRect->left = ioRect->left - halfDx;
	ioRect->right = ioRect->right + halfDx;
	ioRect->top = ioRect->top - halfDy;
	ioRect->bottom = ioRect->bottom - halfDy;

	//check boundary
	if (inZoomFactor>1)
	{
		utl_RectCheckBound(ioRect, imgWidth, imgHeight);
	}
}

///////////////////////////////////////////////////////////////////////////////
void utl_RectSizeAdjust(RECT *ioRect, int rectWidth, int rectHeight, int imgWidth, int imgHeight)
//rect at the same center with different size
{
	int center_x, center_y;
	int halfWid;
	int halfHgt;

	center_x = (ioRect->left+ioRect->right)/2;
	center_y = (ioRect->top+ioRect->bottom)/2;
	halfWid = rectWidth/2;
	halfHgt = rectHeight/2;

	//get out Rect
	ioRect->left	= center_x - halfWid;
	ioRect->right	= center_x + halfWid;
	ioRect->top		= center_y - halfHgt;
	ioRect->bottom	= center_y + halfHgt;

	//check boundary
	utl_RectCheckBound(ioRect, imgWidth, imgHeight);
}

///////////////////////////////////////////////////////////////////////////////
void utl_RectSizeIncrease(RECT *ioRect, int border, int imgWidth, int imgHeight)
//rect at the same center with different size
{
	int center_x, center_y;
	int halfWid;
	int halfHgt;

	center_x = (ioRect->left+ioRect->right)/2;
	center_y = (ioRect->top+ioRect->bottom)/2;
	halfWid = (ioRect->right-ioRect->left)/2 + border;
	halfHgt = (ioRect->bottom-ioRect->top)/2 + border;

	//get out Rect
	ioRect->left	= center_x - halfWid;
	ioRect->right	= center_x + halfWid;
	ioRect->top		= center_y - halfHgt;
	ioRect->bottom	= center_y + halfHgt;

	//check boundary
	utl_RectCheckBound(ioRect, imgWidth, imgHeight);
}

/******************************************************************************/
void utl_RectCheckBound(RECT *ioRect, int imgWidth, int imgHeight)
//check the boundary limit of RECT
{
	if (ioRect->left <0)			ioRect->left=0;
	if (ioRect->left >imgWidth-1)	ioRect->left = imgWidth-1;
	if (ioRect->right <0)			ioRect->right=0;
	if (ioRect->right >imgWidth-1)	ioRect->right = imgWidth-1;

	if (ioRect->top <0)				ioRect->top=0;
	if (ioRect->top >imgHeight-1)	ioRect->top = imgHeight-1;
	if (ioRect->bottom <0)			ioRect->bottom=0;
	if (ioRect->bottom >imgHeight-1) ioRect->bottom = imgHeight-1;
}

///////////////////////////////////////////////////////////////////////////////
void utl_PixelCheckBound(int *pixelX, int *pixelY, int imgWidth, int imgHeight)
//check the boundary limit of pixel
{
	//check x bound
	*pixelX = max(*pixelX, 0);
	*pixelX = min(*pixelX, imgWidth-1);

	//check y bound
	*pixelY = max(*pixelY, 0);
	*pixelY = min(*pixelY, imgHeight-1);
}

///////////////////////////////////////////////////////////////////////////////
void utl_PixelCheckBound_Float(float *pixelX, float *pixelY, int imgWidth, int imgHeight)
//check the boundary limit of pixel
{
	//check x bound
	*pixelX = max(*pixelX, 0);
	*pixelX = min(*pixelX, imgWidth-1);

	//check y bound
	*pixelY = max(*pixelY, 0);
	*pixelY = min(*pixelY, imgHeight-1);
}

///////////////////////////////////////////////////////////////////////////////
void utl_IndexNewDir(char *ioNewDirPath)
//increase index for new dir in case conflict with existing. If dir exists, increase index by 1
{
	char dirCandidate[MAX_PATH];
	char sBufferPath[MAX_PATH];
	int i;
	char strIndex[10];
	int existFlag;

	strcpy(dirCandidate, ioNewDirPath);
	strcat(dirCandidate, "_");			

	//add index
	i=1;
	do{	
		strcpy(sBufferPath, dirCandidate);	
		itoa(i, strIndex, 10);
		strcat(sBufferPath, strIndex);	
		existFlag = _chdir(sBufferPath);
		i++;
	}while( existFlag == 0);

	//return dir path and name			
	strcpy(ioNewDirPath, sBufferPath);
}

⌨️ 快捷键说明

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