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

📄 pro2view.cpp

📁 用VC++写的关于数字图像处理的程序源码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// Pro2View.cpp : implementation of the CPro2View class
//

#include "stdafx.h"
#include "Pro2.h"

#include "Pro2Doc.h"
#include "Pro2View.h"
#include "CDib.h"
#include "DialogIm.h"
#include "math.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPro2View

IMPLEMENT_DYNCREATE(CPro2View, CView)

BEGIN_MESSAGE_MAP(CPro2View, CView)
	//{{AFX_MSG_MAP(CPro2View)
	ON_COMMAND(ID_LINEAR, OnLinear)
	ON_COMMAND(ID_STATISTIC, OnStatistic)
	ON_COMMAND(ID_BALANCE, OnBalance)
	ON_COMMAND(ID_SMOOTH, OnSmooth)
	ON_COMMAND(ID_MIXFILTER, OnMixfilter)
	ON_COMMAND(ID_DEFINITION, OnDefinition)
	ON_COMMAND(ID_PICFILE, OnPicfile)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPro2View construction/destruction

CPro2View::CPro2View()
{
	// TODO: add construction code here
	m_nSupp=3;
	m_nDWTCurDepth=0;
	m_pDbImage=NULL;
	m_nInv=0;
	m_bOne=TRUE;
	m_nRGBBit=3;
	m_sFile=_T("f:\\zhj\\picture\\za.bmp");

	//the below is a strange phenomena that in CString, the character '\' is viewed as '\\',
	//but in CString ,the successional character '\\' is viewed as '\'. It's so confusing.
//	char u='\\';
//	u=m_sFile[2];
//	u=m_sFile[3];
	//

}

CPro2View::~CPro2View()
{
}

BOOL CPro2View::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPro2View drawing

void CPro2View::OnDraw(CDC* pDC)
{
	CPro2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here
	CPoint orig;
	CSize si;

	orig.x=10;
	orig.y=10;
	si.cx=600;
	si.cy=600;

	Dib.Draw(pDC,orig,si);
}

/////////////////////////////////////////////////////////////////////////////
// CPro2View printing

BOOL CPro2View::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CPro2View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CPro2View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CPro2View diagnostics

#ifdef _DEBUG
void CPro2View::AssertValid() const
{
	CView::AssertValid();
}

void CPro2View::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

CPro2Doc* CPro2View::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPro2Doc)));
	return (CPro2Doc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CPro2View message handlers
//////////////////////////////////////////////////////////////////////////////
//calculate Log2

BYTE CPro2View::FloatToByte(double f)
{
	if(f<=0) return (BYTE)0;
	else if(f>=255) return (BYTE)255;
	else return (BYTE)(f+0.5);
}

char CPro2View::FloatToChar(double f)
{
	if(f>=0)
		if(f>=127.0)
			return (char)127;
		else return (char)(f+0.5);
	else
		if(f<=-128)
			return (char)-128;
		else return -(char)(-f+0.5);
}

//************************************linear
void CPro2View::OnLinear() 
{
	// TODO: Add your command handler code here
	pFile=new CFile(m_sFile,CFile::modeReadWrite|CFile::typeBinary);
	
	Dib.Read(pFile);
	Dib.MakePalette();

	Linear(&Dib,0,150,0,255);

	if(m_bOne)
	pFile->Close();

	Invalidate();
	
}

BOOL CPro2View::Linear(CDib* pDib,int Upper1,int Lower1,int Upper2,int Lower2)
{
	int i,j;
	int nWidth=pDib->m_lpBMIH->biWidth;
	int nHeight=pDib->m_lpBMIH->biHeight;

	switch(pDib->m_lpBMIH->biBitCount)
	{
	case 24:
		m_nRGBBit=3;
		break;
	case 32:
		m_nRGBBit=4;
		break;
	default:
		m_nRGBBit=3;
	}

	CSize sizeImageSave=pDib->GetDibSaveDim();

	double *pDbTemp;
	BYTE *pBits;
	int nRGB=0;

	if(!m_pDbImage)	{
			m_pDbImage=new double[nWidth*nHeight];
			if(!m_pDbImage) return FALSE;

	}

	//get data of the source DIB
	for(nRGB=0;nRGB<3;nRGB++)
	{

	for(j=0;j<nHeight;j++)
	{
		pDbTemp=m_pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
		//yes, the below is to get one color value among B,G,R.
			pDbTemp[i]=(double)(*(pBits+i*m_nRGBBit));
		}
	}

	for(int m=0;m<nHeight;m++)
	{
		pDbTemp=m_pDbImage+m*nWidth;
		for(int n=0;n<nWidth;n++)
		{
			pDbTemp[n]=(pDbTemp[n]-Lower1)*(Upper2-Lower2)/(Upper1-Lower1)+Lower2;//m_pDbImage[n+m*nWidth]=m_pDbImage[n+m*nWidth]+150;
		}
	}

	for(j=0;j<nHeight;j++)
	{
		pDbTemp=m_pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
			*(pBits+i*m_nRGBBit)=FloatToByte(pDbTemp[i]);
		}
	}

	}

	return TRUE;
}

//************************************Statistic
BOOL CPro2View::StatChart(CDib* pDib)
{
	int i,j;
	int nWidth=pDib->m_lpBMIH->biWidth;
	int nHeight=pDib->m_lpBMIH->biHeight;

	switch(pDib->m_lpBMIH->biBitCount)
	{
	case 24:
		m_nRGBBit=3;
		break;
	case 32:
		m_nRGBBit=4;
		break;
	default:
		m_nRGBBit=3;
	}

	CSize sizeImageSave=pDib->GetDibSaveDim();

	unsigned char* pDbTemp;
	unsigned char* pDbImage;
	BYTE *pBits;
	int nRGB=0;

	pDbImage=new unsigned char[nWidth*nHeight];

	if(!pDbImage) return FALSE;

	//get data of the source DIB
	for(nRGB=0;nRGB<1;nRGB++)
	{

	for(j=0;j<nHeight;j++)
	{
		pDbTemp=pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
		//yes, the below is to get one color value among B,G,R.
			pDbTemp[i]=(unsigned char)(*(pBits+i*m_nRGBBit));
		}
	}

	unsigned char* lpSrc;

	for(int m=0;m<nHeight;m++)
	{
		pDbTemp=pDbImage+m*nWidth;
		for(int n=0;n<nWidth;n++)
		{
			lpSrc=(unsigned char*)pDbImage+m*nWidth+n;					
			m_Chart.m_nStat[*(lpSrc)]=m_Chart.m_nStat[*(lpSrc)]+1;
		}
	}

/*	for(j=0;j<nHeight;j++)
	{
		pDbTemp=m_pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
			*(pBits+i*m_nRGBBit)=FloatToByte(pDbTemp[i]);
		}
	}
*/
	}

	delete pDbImage;

	return TRUE;
}


void CPro2View::OnStatistic() 
{
	// TODO: Add your command handler code here

	pFile=new CFile(m_sFile,CFile::modeReadWrite|CFile::typeBinary);
	
	Dib.Read(pFile);
	Dib.MakePalette();

	StatChart(&Dib);

	m_Chart.DoModal();

	pFile->Close();

	Invalidate();
}

//************************************Balance
void CPro2View::OnBalance() 
{
	// TODO: Add your command handler code here

	pFile=new CFile(m_sFile,CFile::modeReadWrite|CFile::typeBinary);
	
	Dib.Read(pFile);
	Dib.MakePalette();

	Balance(&Dib);

	m_Chart.DoModal();

	pFile->Close();
	Invalidate();
}

BOOL CPro2View::Balance(CDib* pDib)
{
	int i,j;
	int m,n;
	int nWidth=pDib->m_lpBMIH->biWidth;
	int nHeight=pDib->m_lpBMIH->biHeight;

	switch(pDib->m_lpBMIH->biBitCount)
	{
	case 24:
		m_nRGBBit=3;
		break;
	case 32:
		m_nRGBBit=4;
		break;
	default:
		m_nRGBBit=3;
	}

	CSize sizeImageSave=pDib->GetDibSaveDim();

	unsigned char* pDbTemp;
	unsigned char* pDbImage;
	BYTE *pBits;
	int nRGB=0;

	pDbImage=new unsigned char[nWidth*nHeight];

	if(!pDbImage) return FALSE;

	//get data of the source DIB
	for(nRGB=0;nRGB<1;nRGB++)
	{

	for(j=0;j<nHeight;j++)
	{
		pDbTemp=pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
		//yes, the below is to get one color value among B,G,R.
			pDbTemp[i]=(unsigned char)(*(pBits+i*m_nRGBBit));
		}
	}

	unsigned char* lpSrc;

	for(i=0;i<256;i++)
	{
		m_Chart.m_nStat[i]=0;
	}

	for(m=0;m<nHeight;m++)
	{
		for(n=0;n<nWidth;n++)
		{
			lpSrc=(unsigned char*)pDbImage+m*nWidth+n;					
			m_Chart.m_nStat[*(lpSrc)]=m_Chart.m_nStat[*(lpSrc)]+1;
		}
	}

	int nTemp;

	for(i=0;i<256;i++)
	{
		nTemp=0;
		for(j=0;j<i;j++)
		{
			nTemp+=m_Chart.m_nStat[j];
		}
		m_bal[i]=(BYTE)(nTemp*255/(nWidth*nHeight));
	}

	for(j=0;j<nHeight;j++)
	{
		for(i=0;i<nWidth;i++)
		{
			lpSrc=(unsigned char*)pDbImage+j*nWidth+i;					
			*lpSrc=m_bal[*lpSrc];
		}
	}

	for(j=0;j<nHeight;j++)
	{
		pDbTemp=pDbImage+j*nWidth;
		pBits=pDib->m_lpImage+(nHeight-j-1)*sizeImageSave.cx+nRGB;
		for(i=0;i<nWidth;i++)
		{
			*(pBits+i*m_nRGBBit)=pDbTemp[i];

⌨️ 快捷键说明

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