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

📄 comlib.cpp

📁 图像处理软件,功能比较基础
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// Comlib.cpp: implementation of the CComlib class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "Radar.h"
#include "Comlib.h"

#include "time.h"

#include "math.h"
#include "dibapi.h"

//#include "DMatchingK_L.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

#define   VERSION  "radar.01.7.1"
//////////////////////////////////////////////////////////////////////
// Local functions
//////////////////////////////////////////////////////////////////////
static BOOL ASSERTVALIDPOINTER(void * pointer, CString message)
{
	if(pointer == NULL)
	{
#ifdef _DEBUG
		afxDump << "Error Message " << message << "\n";
#endif
		return FALSE;
	}

	return TRUE;
}

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

CComlib::CComlib()
{
}

CComlib::~CComlib()
{
}

/*************************  ucAverage      ********************************/
/*  To get the average of a 2_dimension character array  */
/*  from (YStart,XStart) with (row,col)                  */
float CComlib::ucAverage(unsigned char **a,int YStart,int XStart,int row,int col, int margin)
{
	register int i,j;
	int start_i,end_i,start_j,end_j;
	float sum = 0.0;

	start_i = YStart + margin;
	start_j = XStart + margin;
	end_i   = row + YStart - margin;
	end_j   = col + XStart - margin;

	for(i=start_i;i<end_i;i++)
	for(j=start_j;j<end_j;j++)
		sum += a[i][j];

	return(sum/((float)(row-2*margin)*(col-2*margin)));
}

/*****************************  ucDeviation   **************************/
/*  To get the deviation of a 2_dimension character array  */
float CComlib::ucDeviation(unsigned char **a,int YStart,int XStart,int row,int col,
						   float average,int margin)
{
	register int i,j;
	int start_i,end_i,start_j,end_j;
	long size = (long)(row - margin * 2) * (col - margin * 2);
	double square_sum = 0.0;

	start_i = YStart + margin;
	start_j = XStart + margin;
	end_i   = row + YStart - margin;
	end_j   = col + XStart - margin;

	for(i=start_i;i<end_i;i++)
	for(j=start_j;j<end_j;j++)
		square_sum += (double)a[i][j] * a[i][j];

	return((float)sqrt((square_sum-size*average*average)/(size-1.0)));
}

BOOL CComlib::CheckFileType(CString file)
{
	BOOL ret = CONT_RAW;

	CString low = file;
	low.MakeLower();
	if(low.Find(".pic")!=-1) ret=CONT_PIC;
	else if( low.Find(".bmp") != -1) ret=CONT_BMP;
	else if( low.Find(".raw") != -1) ret=CONT_RAW;

	return ret;
}

BOOL CComlib::GetImageFileName(CString &file , CString title)
{
	BOOL bType = 0;     // default  .raw

	CFileDialog dlg(TRUE,".pic" ,file ,OFN_HIDEREADONLY ); //,ext, NULL);
	dlg.m_ofn.lpstrTitle = title;
	if(dlg.DoModal() == IDOK)
	{
		file = dlg.GetPathName();
		bType = CheckFileType(file);
	}
	else
		bType = CheckFileType(file);

	return bType;
}

/*****************************   fspace_1d  **************************/
/*  To allocation a 1_dimension dynamic array  */
void * CComlib::fspace_1d(int col, int length)
{
	void *b;

	b = (void *)calloc(length,col);

	CString  err="  Sorry!  not enough space for you.\n";
	ASSERTVALIDPOINTER(b, err);

	return(b);
}

/*************      fspace_2d    ***************************************/
/*  To allocation a 2_dimension dynamic array  */
void ** CComlib::fspace_2d(int row,int col,int lenth)
{
	int i;
	void **b;

	b = (void **)calloc(sizeof(void *),row);

	CString  err="  Sorry!  not enough space for you.\n";
	ASSERTVALIDPOINTER(b, err);

	for(i=0;i<row;i++)
	{
		b[i] = (void *)calloc(lenth,col*lenth);
		ASSERTVALIDPOINTER(b[i], err);
	}

	return(b);
}

/*************      fspace_3d    ***************************************/
/*  To allocation a 3_dimension dynamic array  */
void *** CComlib::fspace_3d(int row1,int row2,int row3,int lenth)
{
	int i;
	void ***b;

	b = (void ***)calloc(sizeof(void **),row1);

	CString  err="  Sorry!  not enough space for you.\n";
	ASSERTVALIDPOINTER(b, err);

	for(i=0;i<row1;i++)
		b[i] = (void **)fspace_2d(row2,row3,lenth);

	return(b);
}

/*******************************  ffree_1d   ****************************/
/*  To free a 1_dimension dynamic array  */
void CComlib::ffree_1d(void *a)
{
	if(a = NULL) return;

	free(a);

	a = NULL;
}

/*******************************  ffree_2d   ****************************/
/*  To free a 2_dimension dynamic array  */
void CComlib::ffree_2d(void **a,int row)
{
	if(a==NULL) return ;

	int i;

	for(i=0;i<row;i++) 
		free(a[i]);
	free(a);

	a = NULL;
}

/*******************************  ffree_3d   ****************************/
/*  To free a 3_dimension dynamic array  */
void CComlib::ffree_3d(void ***a,int row1,int row2)
{
	if(a==NULL) return ;
	int i;

	for(i=0;i<row1;i++)
		ffree_2d((void **)a[i],row2);
	free(a);

	a = NULL;
}

//////////////////////////////////////////////////////////////////

/**************************  wfwrite1    *****************************/
/* To open a file to write data in */
FILE *CComlib::wfwrite1(int row,int col,int type, CString filename)
{
	int i;
	unsigned char head_byte[64];

	FILE *fp = fopen(filename,"wb+"); 
	CString err;
	err.Format(" %s is not created!\n" ,filename);
	if(! ASSERTVALIDPOINTER(fp, err)) return NULL;

	for(i=0;i<64;i++) head_byte[i] = 0;

	head_byte[0] = 'I';  /* image type. 'I':b/w image, 'C':color image */
	head_byte[1] = 'M';
	head_byte[2] = head_byte[3] = 0;  /* comment space */
	head_byte[4] = col % 256;
	head_byte[5] = col / 256;  /* column size */
	head_byte[6] = row % 256;
	head_byte[7] = row / 256;  /* row size */
	head_byte[8] = head_byte[9] = 0;  /* column start */
	head_byte[10] = head_byte[11] = 0;  /* row start */
	head_byte[12] = head_byte[13] =0;
	head_byte[14] = type % 256;
	head_byte[15] = type / 256;  /* data type. 1:unsigned char, 2:int,   */
							   /*            3:float,         4:double */
	if(col*row != 0)  fwrite(head_byte,64,1,fp);

	return(fp);
}

/*****************   wfread1  ***********************/
/*  To open data file to read from, the file name is a parameter */
/*  NOT USED  */
FILE *CComlib::wfread1(int *row,int *col,int *type, CString filename)
{
	unsigned char Tem1,Tem2;  //  Used for VC
	FILE *fp = fopen(filename,"rb");

	CString err;
	err.Format(" %s is not created!\n" ,filename);
	if(! ASSERTVALIDPOINTER(fp, err)) return NULL;

	char FType[2];
	fread(FType,sizeof(unsigned char),2,fp);
	if(FType[0]!='I' || FType[1]!='M')
	{
		fclose(fp);
		return NULL;
	}
	fseek(fp,4,SEEK_SET);
	//fread(col,sizeof(int),1,fp);
	//fread(row,sizeof(int),1,fp);
	fread(&Tem1,sizeof(unsigned char),1,fp);   //  Used for VC
	fread(&Tem2,sizeof(unsigned char),1,fp);   //  Used for VC
	*col = (int)Tem2 * 256 + Tem1;             //  Used for VC
	fread(&Tem1,sizeof(unsigned char),1,fp);   //  Used for VC
	fread(&Tem2,sizeof(unsigned char),1,fp);   //  Used for VC
	*row = (int)Tem2 * 256 + Tem1;             //  Used for VC
	//fseek(fp,14,SEEK_SET);
	//fread(type,sizeof(int),1,fp);
	fread(&Tem1,sizeof(unsigned char),1,fp);   //  Used for VC
	fread(&Tem2,sizeof(unsigned char),1,fp);   //  Used for VC
	*type = (int)Tem2 * 256 + Tem1;            //  Used for VC
	fseek(fp,64,SEEK_SET);

	CFile dfile( filename, CFile::shareDenyWrite | CFile::modeRead | CFile::typeBinary);
	int Length = dfile.GetLength();

	if( Length != (*row)*(*col) + 64 )
	{
		fclose(fp);
		return NULL;
	}
	else
		return(fp);
	//printf("  Samples or column: %d . Lines or row: %d\n",*col,*row);
}

/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
unsigned char *CComlib::InputImageWithName(/*int *Row,int *Col, */CString FileName)
{
	int           Type; //RowNo,
	unsigned char *Image;
	FILE          *fp;
	int Row;
	int Col;

	fp = wfread1(&Row,&Col,&Type,FileName);
	if(fp == NULL) return NULL;

	Image = (unsigned char *)fspace_1d( (long) Row * Col,sizeof(unsigned char));
	fread(Image,sizeof(unsigned char),(long) Row * Col,fp);
	fclose(fp);

	return(Image);
}

CSize CComlib::GuessSize(int Len)
{
	int r , c;
	for( r = (int) sqrt(Len) ; r > 1 ; r--)
	{
		if( Len%r == 0 ) 
		{
			c = Len/r;
			if(r*c == Len) 
				return CSize(r,c);
		}
	}

	return CSize(-1,-1);
}

int CComlib::GuessImageSizeWithName(int *Row,int *Col, CString FileName, BOOL bIsPic)
{
	if( bIsPic )
	{
		int  Type;
		FILE * fp = wfread1(Row,Col,&Type,FileName);
		if(fp == NULL)	return -1;
		else  fclose(fp);

		CFile dfile( FileName, CFile::shareDenyWrite | CFile::modeRead | CFile::typeBinary) ;
		int Length = dfile.GetLength();

		if( Length != (*Row)*(*Col) + 64 ) 
			return -1 ;
		else
			return Length - 64;
	}

	TRY
	{
		CFile dfile( FileName, CFile::shareDenyWrite | CFile::modeRead | CFile::typeBinary) ;
		int Length = dfile.GetLength();
		
		CSize rc = GuessSize(Length);

		*Row = rc.cx;
		*Col = rc.cy;

		return Length;
	}
	CATCH( CFileException, e )
	{
    #ifdef _DEBUG
        afxDump << "File could not be opened " << e->m_cause << "\n";
    #endif
	}
	END_CATCH

	return  -1 ;
}

/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
unsigned char **CComlib::InputImageWithName(int *Row,int *Col, CString FileName)
{
	int           RowNo,Type;
	unsigned char **Image;
	FILE          *fp;

	fp = wfread1(Row,Col,&Type,FileName);
	if(fp == NULL) 
	{
	  *Row = *Col = -1;
	  return NULL;
	}

	Image = (unsigned char **)fspace_2d(*Row,*Col,sizeof(unsigned char));
	for(RowNo=0;RowNo<*Row;RowNo++)
		fread(Image[RowNo],sizeof(unsigned char),*Col,fp);
	fclose(fp);

	return(Image);
}

/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
BOOL CComlib::InputImageWithName(IMAGE *p, CString FileName)
{
	p->init();

	int           RowNo,Type;
	FILE          *fp;

	fp = wfread1(&(p->Row),&(p->Col),&Type,FileName);
	if(fp == NULL)
	{
		p->Row = p->Col= -1;
		return FALSE;
	}

	p->Image = (unsigned char **)fspace_2d(p->Row,p->Col,sizeof(unsigned char));
	for(RowNo=0;RowNo<p->Row;RowNo++)
		fread(p->Image[RowNo],sizeof(unsigned char),p->Col,fp);
	fclose(fp);

	return TRUE;
}

/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to double array Image      */
double *CComlib::InputImageWithName_d(int *Row,int *Col, CString FileName)
{
	unsigned char * img = InputImageWithName_1d(Row,Col,FileName);
	if(img == NULL) 
	{
		*Row = *Col = -1;
		return NULL;
	}

	double * Image = (double *)fspace_1d((*Row)*(*Col),sizeof(double));

	for(int k=0;k<(*Row)*(*Col); k++)
		Image[k] = img[k];


	return(Image);
}


/********************  INPUTIMAGEWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
unsigned char *CComlib::InputImageWithName_1d(int *Row,int *Col, CString FileName)
{
	int           RowNo,Type;
	unsigned char *Image;
	FILE          *fp;

	fp = wfread1(Row,Col,&Type,FileName);
	if(fp == NULL) 
	{
		*Row = *Col = -1;
		return NULL;
	}

	Image = (unsigned char *)fspace_1d((*Row)*(*Col),sizeof(unsigned char));
	fread(Image,sizeof(unsigned char) , (*Row)*(*Col) , fp);
	fclose(fp);

	return(Image);
}

/********************  INPUTIMAGEDATAWITHNAME  **********************/
/*  Input a image with name FileName to array Image             */
void **CComlib::InputImageDataWithName(int Row,int Col, CString FileName, int HeadBytes , int nPixelBytes)
{
	void **Image = NULL;
	
	TRY
	{
		CFile dfile( FileName, CFile::shareDenyWrite | CFile::modeRead | CFile::typeBinary) ;
		if(dfile.GetLength() != (DWORD) (Row*Col)*nPixelBytes + HeadBytes)  
		{
			TRACE("\n Invalid data file or missing row , col!");
			return NULL;
		}

		Image = (void **)fspace_2d(Row, Col, nPixelBytes);

		dfile.Seek( HeadBytes, CFile::begin );

		for(int i=0;i<Row;i++)
			dfile.ReadHuge(Image[i] , Col*nPixelBytes);
		dfile.Close();
	}
	CATCH( CFileException, e )
	{
    #ifdef _DEBUG
        afxDump << "File could not be opened " << e->m_cause << "\n";
    #endif
	}
	END_CATCH

	return(Image);
}

BOOL CComlib::OutputImageWithName(unsigned char *Image,int Row, int Col, CString FileName)
{
	FILE       *fp;
	if(Image ==NULL) return FALSE;

	fp = wfwrite1(Row,Col,1,FileName);
	if(fp == NULL) return FALSE;
	long aa = fwrite(Image, sizeof(unsigned char),(long) Row*Col,fp);
	fclose(fp);

	if(aa != (long)Row*Col) return FALSE;

	return TRUE;
}

BOOL CComlib::OutputImageWithName(IMAGE p, CString FileName)
{
	FILE       *fp;
	if(p.Image==NULL) return FALSE;

	fp = wfwrite1(p.Row,p.Col,1,FileName);
	if(fp == NULL) return FALSE;
	long aa = fwrite(p.Image, sizeof(unsigned char),(long) p.Row*p.Col,fp);
	fclose(fp);

	if(aa != (long)p.Row*p.Col) return FALSE;

	return TRUE;
}

BOOL CComlib::OutputImageDataWithName(void **Image,int Row, int Col, CString FileName, int HeadBytes, int nPixelBytes )
{
	if(Image ==NULL) return FALSE;

	// 保存为数据文件
	TRY
	{
		CFile f( FileName, CFile::modeCreate | CFile::modeWrite );
		
		f.Seek( HeadBytes, CFile::begin );

		for(int i = 0;i<Row;i++)
			f.Write(Image[i],Col * nPixelBytes);
		f.Close();
	}
	CATCH( CFileException, e )
	{
    #ifdef _DEBUG
        afxDump << "File could not be opened " << e->m_cause << "\n";
    #endif
	}
	END_CATCH


	return TRUE;
}

/********************  OUTPUTIMAGEWITHNAME  **********************/
/*  Output a image with name FileName to array Image             */
/*  Notes: Image must be a space allocated pointer               */
BOOL CComlib::OutputImageWithName(unsigned char **Image,int Row,int Col, CString FileName)
{
	int        RowNo;
	FILE       *fp;

	if(Image ==NULL) return FALSE;

	fp = wfwrite1(Row,Col,1,FileName);
	if(fp == NULL) return FALSE;
	long aa=0;
	for(RowNo=0;RowNo<Row;RowNo++)
		aa += fwrite(Image[RowNo],sizeof(unsigned char),Col,fp);
	fclose(fp);

	return TRUE;
}

COORDINATE * CComlib::GenRandomPost( int &n, int x1, int x2, int y1, int y2, int step)
{
	COORDINATE * p = (COORDINATE *) fspace_1d(n, sizeof(COORDINATE) );
	ASSERT(p != NULL);

	if(step < 1)
		step = 1;

	int m=0;
	for(int k=y1; k<=y2;k += step)
	for(int j=x1,i=0; j<=x2 && i<(int)(sqrt((double)n)+0.5);j += step,i++)
	{
		if(n > m)
		{
			p[m].x = j;
			p[m].y = k;
			p[m].numb = m;

			TRACE("\n x[%d]= %d  y[%d]= %d",m,j,m,k);
			m++;
		}
		else
		{
			k = y2 ; 
			break ;
		}
	}

	n = m;
	if(n<1)
		AfxMessageBox(" Out of region !");

	return  p;
}

void CComlib::OutputPostData(COORDINATE * p, int n, CString txtfile)
{
	FILE *fp = fopen(txtfile, "wt+"); 
	
	CString err;
	err.Format(" txtfile is not created!\n");

⌨️ 快捷键说明

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