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

📄 global.c

📁 此代码是C编的EWZ的测试程序,大家可以尝试对这个包里lena.pgm进行图像压缩处理.
💻 C
字号:
/*--------------------------------------------------------------------------------------------*/
/*
 * Mow-Song, Ng 2/9/2002
 * msng@mmu.edu.my
 * http://www.pesona.mmu.edu.my/~msng
 *
 * I do not claim copyright to the code, but if you use them or modify them,
 * please drop me a mail.
 *
 */
/*--------------------------------------------------------------------------------------------*/

#include "global.h"

/*--------------------------------------------------------------------------------------------*/
Real MOD (Real x, Real N)
{
   Real xmodN = x - N*((int)(x/N));
   if (xmodN < 0) xmodN += N;
   return xmodN;
}


/*--------------------------------------------------------------------------------------------*/
Real square (Real x)
{
	return (x*x);
}

/*--------------------------------------------------------------------------------------------*/
int isquare (int x)
{
	return (x*x);
}

/*--------------------------------------------------------------------------------------------*/
int sign(Real x)
{
	return (x > 0 ? 1 : x < 0 ? -1 : 0);
}

/*---------------------------------------------------------------------------*/
/*---------------------------------------------------------------------------*/
/* maximum power of 2 integer less than or equal x */
/* 0 <= x < 2, return 0 */
/* 2 <= x < 4, return 1 */
/* 4 <= x < 8, return 2 */
/* 8 <= x < 16, return 3 */
/* and so on... */
int Log2(int x)
{
   int count = 0;

   while (x > 1)  {
      x >>= 1;
      count++;
   }
   return count;
}

/*---------------------------------------------------------------------------*/
/* number of bits to represent the magnitude of x */
/* 0 <= x < 2, return 1 */
/* 2 <= x < 4, return 2 */
/* 4 <= x < 8, return 3 */
/* and so on... */
int NumberOfBits(int x)
{ 
	int count = 1;

	/* make positive */
	if (x < 0){
		x = -x;
	}

   while (x > 1)  {
      x >>= 1;
      count++;
   }
   return count;
}

/*---------------------------------------------------------------------------*/
Real FindMax(Real *input, int size)
{
	int i;
	Real Max=-MaxReal;

	for (i=0; i<size; i++){
		if (input[i]>Max){
			Max = input[i];
		}
	}
	return Max;
}

/*---------------------------------------------------------------------------*/
int iFindMax(int *input, int size)
{
	int i;
	int Max=input[0];

	for (i=1; i<size; i++){
		if (input[i]>Max){
			Max = input[i];
		}
	}
	return Max;
}

/*---------------------------------------------------------------------------*/
Real FindAbsMax(Real *input, int size)
{
	int i;
	Real Max=-MaxReal;

	for (i=0; i<size; i++){
		if (fabs(input[i])>Max){
			Max = (Real)fabs(input[i]);
		}
	}
	return Max;
}

/*---------------------------------------------------------------------------*/
int iFindAbsMax(int *input, int size)
{
	int i;
	int Max=abs(input[0]);

	for (i=1; i<size; i++){
		if (abs(input[i])>Max){
			Max = abs(input[i]);
		}
	}
	return Max;
}

/*---------------------------------------------------------------------------*/
Real FindMin(Real *input, int size)
{
	int i;
	Real Min=MaxReal;

	for (i=0; i<size; i++){
		if (input[i]<Min){
			Min=input[i];
		}
	}
	return Min;
}

/*---------------------------------------------------------------------------*/
int iFindMin(int *input, int size)
{
	int i;
	int Min=input[0];

	for (i=1; i<size; i++){
		if (input[i]<Min){
			Min=input[i];
		}
	}
	return Min;
}

/*---------------------------------------------------------------------------*/
Real Sum(Real *input, int size)
{
	int i;
	Real sum=0.0;

	for (i=0; i<size; i++){
		sum += *input++;
	}

	return sum;
}


/*---------------------------------------------------------------------------*/
void AddValue(Real *input, int size, double value)
{
	int i;

	for (i=0; i<size; i++){
		*input++ += (Real) value;
	}

	return;
}

/*---------------------------------------------------------------------------*/
void ScaleArray(Real *input, int size, double scale)
{
	int i;

	for (i=0; i<size; i++){
		*input++ /= (Real) scale;
	}

	return;
}

/*---------------------------------------------------------------------------*/
void FindMaxMin(Real *input, int size, Real *Max, Real *Min)
{
	int i;
	*Min=MaxReal;
	*Max=-MaxReal;

	for (i=0; i<size; i++){
		if (input[i]< *Min){
			*Min=input[i];
		}

		if (input[i]> *Max){
			*Max=input[i];
		}
	}
}

/*---------------------------------------------------------------------------*/
void iFindMaxMin(int *input, int size, int *Max, int *Min)
{
	int i;
	*Min=input[0];
	*Max=input[0];

	for (i=1; i<size; i++){
		if (input[i]< *Min){
			*Min=input[i];
		}

		if (input[i]> *Max){
			*Max=input[i];
		}
	}
}

/*---------------------------------------------------------------------------*/
void WaitKey(void)
{
	getch();
}

/*----------------------------------------------------------------------------*/
int GetFileLength (char *FileName)
{
   struct stat statistics; /* input file statistics */

   if(stat(FileName,&statistics) == -1) return 0;

   return (int)statistics.st_size;
}


/*--------------------------------------------------------------------------------------------*/	
/* function for fixing the precision of floating number. Used in storing the floating point
 * parameters. These functions should be used with care. Overflow may occur and we 
 * have no mechanism to guard this yet.
 *
 */
double RoundReal(double x, int precision)
{
	return (double)(RINT(x*(1<<precision)))/(1<<precision);
}

/*--------------------------------------------------------------------------------------------*/	
int realToInt(double x, int precision)
{
	return RINT(x*(1<<precision));	
}

/*--------------------------------------------------------------------------------------------*/	
double intToReal(int i, int precision)
{
	return ((double)(i))/(1<<precision);	
}

/*--------------------------------------------------------------------------------------------*/	
void ReadLine(char *prompt, char *line)
{
	if (prompt!=NULL){
		fprintf(stdout, "\n%s ", prompt);
	}
	if (!gets(line)){
		Error("ReadLine: Fail to read line.\n");
	}
}

/*--------------------------------------------------------------------------------------------*/	
int ReadInteger(char *prompt)
{
	int a;
	char line[80];
	for (;;){
		if (prompt!=NULL){
			fprintf(stdout, "\n%s ", prompt);
		}
		if (!gets(line)){
			Error("ReadIngeter: Fail to read line.\n");
		}
		if (sscanf(line, "%d", &a)==1){
			break;
		}
	}
	return a;
}
	
/*--------------------------------------------------------------------------------------------*/	
Real ReadReal(char *prompt)
{
	return (Real)ReadDouble(prompt);
}

/*--------------------------------------------------------------------------------------------*/	
double ReadDouble(char *prompt)
{
	float a;
	char line[80];

	for (;;){
		if (prompt!=NULL){
			fprintf(stdout, "\n%s ", prompt);
		}
		if (!gets(line)){
			Error("ReadDouble: Fail to read line.\n");
		}
		if(sscanf(line, "%f", &a)==1){
			break;
		}
	}
	return (double)a;
}

/*--------------------------------------------------------------------------------------------*/	
int ReadYesNo(char *prompt)
{
	char line[80];

	for (;;){	
		if (prompt!=NULL){
			fprintf(stdout, "\n%s (y/n) ", prompt);
		}
		if (!gets(line)){
			Error("ReadLine: Fail to read line.\n");
		}
		if (line[0]=='y' || line[0]=='n'){
			break;
		}
	}

	return (line[0]=='y' ? 1 : 0);
}

/*--------------------------------------------------------------------------------------------*/	
void Error(char *fmt, ...)
{
	va_list argptr;

	va_start( argptr, fmt );
	fprintf(stderr, "Error: " );
	vprintf( fmt, argptr );
	va_end( argptr );
	exit( -1 );
}

/*--------------------------------------------------------------------------------------------*/	
void Warning(char *fmt, ...)
{
	va_list argptr;

	va_start( argptr, fmt );
	fprintf( stderr, "Warning: " );
	vprintf( fmt, argptr );
	va_end( argptr );
}

/*--------------------------------------------------------------------------------------------*/	
/* Note that if we want to use array notations, we need to allocate memory for pointers. */
/* So we just do a linear array and use arithmetic to access the memory. */
ArrayReal *ArrayRealAlloc(int xsize, int ysize, int zsize)
{
	ArrayReal *A;
	
	if ((A = (ArrayReal *)malloc(sizeof(ArrayReal)))==NULL){
		Error("ArrayReal: memory allocation error.\n");
	}

	if ((A->a = (REAL *)calloc(xsize*ysize*zsize, sizeof(REAL)))==NULL){
		Error("ArrayReal: memory allocation error.\n");
	}
	
	A->xsize = xsize;
	A->ysize = ysize;
	A->zsize = zsize;
	
	return A;
}

/*--------------------------------------------------------------------------------------------*/	
void ArrayRealFree(ArrayReal *A)
{
	free(A->a);
	free(A);
}

/*--------------------------------------------------------------------------------------------*/	
REAL ArrayRealGetVal(ArrayReal *A, int x, int y, int z)
{
	return (A->a[z*(A->xsize*A->ysize) + y*A->xsize + x]);
}

/*--------------------------------------------------------------------------------------------*/	
void ArrayRealPutVal(ArrayReal *A, int x, int y, int z, REAL val)
{
	A->a[z*(A->xsize*A->ysize) + y*A->xsize + x] = val;
}

/*--------------------------------------------------------------------------------------------*/
ArrayUChar *ArrayUCharAlloc(int xsize, int ysize, int zsize)
{
	ArrayUChar *A;
	
	if ((A = (ArrayUChar *)malloc(sizeof(ArrayUChar)))==NULL){
		Error("ArrayUChar: memory allocation error.\n");
	}

	if ((A->a = (UCHAR *)calloc(xsize*ysize*zsize, sizeof(UCHAR)))==NULL){
		Error("ArrayUChar: memory allocation error.\n");
	}

	A->xsize = xsize;
	A->ysize = ysize;
	A->zsize = zsize;
	return A;
}

/*--------------------------------------------------------------------------------------------*/
void ArrayUCharFree(ArrayUChar *A)
{
	free(A->a);
	free(A);
}

/*--------------------------------------------------------------------------------------------*/
UCHAR ArrayUCharGetVal(ArrayUChar *A, int x, int y, int z)
{
	return (A->a[z*(A->xsize*A->ysize) + y*A->xsize + x]);
}

/*--------------------------------------------------------------------------------------------*/
void ArrayUCharPutVal(ArrayUChar *A, int x, int y, int z, UCHAR val)
{
	A->a[z*(A->xsize*A->ysize) + y*A->xsize + x] = val;
}

/*--------------------------------------------------------------------------------------------*/
ArrayInt32 *ArrayInt32Alloc(int xsize, int ysize, int zsize)
{
	ArrayInt32 *A;
	
	if ((A = (ArrayInt32 *)malloc(sizeof(ArrayInt32)))==NULL){
		Error("ArrayInt32: memory allocation error.\n");
	}

	if ((A->a = (INT32 *)calloc(xsize*ysize*zsize, sizeof(INT32)))==NULL){
		Error("ArrayInt32: memory allocation error.\n");
	}

	A->xsize = xsize;
	A->ysize = ysize;
	A->zsize = zsize;
	return A;
}

/*--------------------------------------------------------------------------------------------*/
void ArrayInt32Free(ArrayInt32 *A)
{
	free(A->a);
	free(A);
}

/*--------------------------------------------------------------------------------------------*/
INT32 ArrayInt32GetVal(ArrayInt32 *A, int x, int y, int z)
{
	return (A->a[z*(A->xsize*A->ysize) + y*A->xsize + x]);
}

/*--------------------------------------------------------------------------------------------*/
void ArrayInt32PutVal(ArrayInt32 *A, int x, int y, int z, INT32 val)
{
	A->a[z*(A->xsize*A->ysize) + y*A->xsize + x] = val;
}

/*--------------------------------------------------------------------------------------------*/
/*--------------------------------------------------------------------------------------------*/
void InitTimer(Timer *c)
{
	c->elp = 0;
	c->stat = 0; 
}

/*--------------------------------------------------------------------------------------------*/
void ResetTimer(Timer *c)
{ 
	c->elp = 0;
	c->stat = 0; 
}

/*--------------------------------------------------------------------------------------------*/
void StartTimer(Timer *c, char *s)
{
	if (s != NULL){
		puts(s);
	}
	
	if (c->stat){
		Warning("Timer already on!\n");
	}
	else{
		c->mark = clock();  
		c->stat = 1; 
	}
}

/*--------------------------------------------------------------------------------------------*/
void StopTimer(Timer *c)
{
	if (c->stat) {
		c->elp += clock() - c->mark;  
		c->stat = 0; 
	}
	else{
    Warning("Timer already off!\n");
	}
}

/*--------------------------------------------------------------------------------------------*/
REAL ReadTimer(Timer *c)
{
	return (REAL)(c->stat ? c->elp + (clock() - c->mark) : c->elp) / CLOCKS_PER_SEC;
}

/*--------------------------------------------------------------------------------------------*/
void DisplayTimer(Timer *c, char *s)
{
	REAL sc;
	int hr;
	int mn;

	sc = (REAL)(c->stat ? c->elp + (clock() - c->mark) : c->elp) / CLOCKS_PER_SEC;
	hr = (int)(sc / 3600.0);  
	sc -= (REAL)3600.0 * hr;
	mn = (int)(sc / 60.0);  
	sc -= (REAL)60.0 * mn;
 
	if (s != NULL){
		fprintf(stdout, "%s ", s);
	}
	
	if (hr) {
		fprintf(stdout, "%d hour", hr);
		if (hr > 1){
			fprintf(stdout, "s, ");
		}
		else{
			fprintf(stdout, ", "); 
		}
	}
	
	if ((hr) || (mn)) {
		fprintf(stdout, "%d minute", mn);
		if (mn > 1){
			fprintf(stdout, "s, and ");
		}
		else{
			fprintf(stdout, ", and "); 
		}
	}
	
	fprintf(stdout, "%5.5f seconds.\n", sc);
}
/*--------------------------------------------------------------------------------------------*/

/*--------------------------------------------------------------------------------------------*/
void ExtractProgramName(char *NameOnly, char *FullPath)
{
	char *c;

	/* hack the program name */
	c = strrchr(FullPath, '\\');
	c++;
	sprintf(NameOnly, c);
	c = strrchr(NameOnly, '.');
	*c = 0;
}

/*--------------------------------------------------------------------------------------------*/

⌨️ 快捷键说明

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