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

📄 imggrabprocdoc.cpp

📁 《医学图象的远程传输系统》
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// ImgGrabProcDoc.cpp : implementation of the CImgGrabProcDoc class
//

#include "stdafx.h"
#include "ImgGrabProc.h"

#include "ImgGrabProcDoc.h"
#include "GaussDlg.h"
#include "MeFilterModeDlg.h"
#include "HistogramsDlg.h"
#include "LinerDlg.h"
#include "WindowTran.h"
#include "GeometryDlg.h"

#include "Templat.h"


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

#include "ImgPropDlg.h"

//the dib file's default size
#define DIBWIDTH 632
#define DIBHEIGHT 474
//the raw file's default size
#define RAWWIDTH 1024
#define RAWHEIGHT 1024

#define BOUND(x,y,z) ((x)<(y))?(y):(((x)>(z))?(z):(x))//y=<x=<z 
#define WINTRAN(x,l,h) (x<h)?((x>l)?x:0):255

/////////////////////////////////////////////////////////////////////////////
// CImgGrabProcDoc

IMPLEMENT_DYNCREATE(CImgGrabProcDoc, CDocument)

BEGIN_MESSAGE_MAP(CImgGrabProcDoc, CDocument)
	//{{AFX_MSG_MAP(CImgGrabProcDoc)
	ON_COMMAND(ID_GAUSS_SMOOTH, OnGaussSmooth)
	ON_COMMAND(ID_MEDIAN_FILTER, OnMedianFilter)
	ON_COMMAND(ID_GRAD_SHARP, OnGradSharp)
	ON_COMMAND(ID_HIGH_FILTER, OnHighFilter)
	ON_COMMAND(ID_HISTOGRAM, OnHistogram)
	ON_COMMAND(ID_DOT_LINER, OnDotLiner)
	ON_COMMAND(ID_WINDOW_TRANS, OnWindowTrans)
	ON_COMMAND(ID_GRAD_AVG, OnGradAvg)
	ON_COMMAND(ID_GEOMETRY_TRAN, OnGeometryTran)
	ON_COMMAND(ID_FILE_SAVEAS256, OnFileSaveas256)
	ON_COMMAND(ID_FILE_SAVEAS24, OnFileSaveas24)
	ON_COMMAND(ID_SOBEL_CONVO, OnSobelConvo)
	ON_COMMAND(ID_PROC_CONVOLUTE, OnProcConvolute)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImgGrabProcDoc construction/destruction

CImgGrabProcDoc::CImgGrabProcDoc()
{
	m_pDib=NULL;
	m_bmInfo.bmiHeader.biBitCount=24;
	m_bmInfo.bmiHeader.biClrImportant=0;
	m_bmInfo.bmiHeader.biClrUsed=0;
	m_bmInfo.bmiHeader.biCompression=0;
	m_bmInfo.bmiHeader.biHeight=0;
	m_bmInfo.bmiHeader.biPlanes=1;
	m_bmInfo.bmiHeader.biSize=sizeof(BITMAPINFOHEADER);
	m_bmInfo.bmiHeader.biSizeImage=0;
	m_bmInfo.bmiHeader.biWidth=0;
	m_bmInfo.bmiHeader.biXPelsPerMeter=1;
	m_bmInfo.bmiHeader.biYPelsPerMeter=1;
//Gauss 
	Gauss[0]=1;Gauss[1]=2;Gauss[2]=1;
	Gauss[3]=2;Gauss[4]=4;Gauss[5]=2;
	Gauss[6]=1;Gauss[7]=2;Gauss[8]=1;

}

CImgGrabProcDoc::~CImgGrabProcDoc()
{
}

BOOL CImgGrabProcDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)

	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CImgGrabProcDoc serialization

void CImgGrabProcDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CImgGrabProcDoc diagnostics

#ifdef _DEBUG
void CImgGrabProcDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CImgGrabProcDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CImgGrabProcDoc commands

BOOL CImgGrabProcDoc::OnOpenDocument(LPCTSTR lpszPathName) 
{
	if (!CDocument::OnOpenDocument(lpszPathName))
		return FALSE;
	
	if (strstr(lpszPathName,".dib")!=NULL){
		//if this image is the format of dib
		if (!OpenDibFile(lpszPathName)){
			if (m_pDib){
				::GlobalFree(m_pDib);
				m_pDib=NULL;
			}
			m_bmInfo.bmiHeader.biWidth=0;
			m_bmInfo.bmiHeader.biHeight=0;
		}
	}
	else if (strstr(lpszPathName,".raw")!=NULL){
		//if this image is the format of raw
		if (!OpenRawFile(lpszPathName)){
			if (m_pDib){
				::GlobalFree(m_pDib);
				m_pDib=NULL;
			}
			m_bmInfo.bmiHeader.biWidth=0;
			m_bmInfo.bmiHeader.biHeight=0;
		}
	}
	else{
		//this image must be a bitmap 
		if (!OpenBmpFile(lpszPathName)){
			if (m_pDib){
				::GlobalFree(m_pDib);
				m_pDib=NULL;
			}
			m_bmInfo.bmiHeader.biWidth=0;
			m_bmInfo.bmiHeader.biHeight=0;
		}
	}
	return TRUE;
}

BOOL CImgGrabProcDoc::OpenDibFile(LPCTSTR lpszPathName)
//open a dib image and save it in the m_pDib
//2002.04.27
{
	CImgPropDlg pDlg;
	pDlg.m_ImgWidth=DIBWIDTH;
	pDlg.m_ImgHeight=DIBHEIGHT;
	if (pDlg.DoModal()!=IDOK){
		AfxMessageBox("User canceled!");
		return FALSE;
	}
	ULONG DibWidth=pDlg.m_ImgWidth;
	ULONG DibHeight=pDlg.m_ImgHeight;
	if (m_pDib){
		::GlobalFree(m_pDib);
	}
	m_pDib=(BYTE*)::GlobalAlloc(GMEM_FIXED,DibWidth*DibHeight*3);
	if (!m_pDib){
		AfxMessageBox("Failed to allocate memory!");
		return FALSE;
	}
	CFile f;
	if (!f.Open(lpszPathName,CFile::modeRead)){
		::GlobalFree(m_pDib);
		m_pDib=NULL;
		AfxMessageBox("Can not open file!");
		return FALSE;
	}
	if (f.ReadHuge(m_pDib,DibWidth*DibHeight*3)!=(DibWidth*DibHeight*3)){
		::GlobalFree(m_pDib);
		m_pDib=NULL;
		AfxMessageBox("Can not read from file!");
		return FALSE;
	}
	f.Close();
	m_bmInfo.bmiHeader.biWidth=DibWidth;
	m_bmInfo.bmiHeader.biHeight=DibHeight;
	return TRUE;
}

BOOL CImgGrabProcDoc::OpenRawFile(LPCTSTR lpszPathName)
//open a raw image and save it in m_pDib
//2002.04.27
{
	CImgPropDlg pDlg;
	pDlg.m_ImgWidth=RAWWIDTH;
	pDlg.m_ImgHeight=RAWHEIGHT;
	if (pDlg.DoModal()!=IDOK){
		AfxMessageBox("User canceled");
		return FALSE;
	}
	ULONG RawWidth=pDlg.m_ImgWidth;
	ULONG RawHeight=pDlg.m_ImgHeight;
	if (m_pDib){
		::GlobalFree(m_pDib);
	}
	m_pDib=(BYTE*)::GlobalAlloc(GMEM_FIXED,RawWidth*RawHeight*3);
	if (!m_pDib){
		AfxMessageBox("Failed to allocate memory!");
		return FALSE;
	}
	CFile f;
	if (!f.Open(lpszPathName,CFile::modeRead)){
		::GlobalFree(m_pDib);
		m_pDib=NULL;
		AfxMessageBox("Can not open file!");
		return FALSE;
	}
	if (f.ReadHuge(m_pDib,RawWidth*RawHeight)!=(RawWidth*RawHeight)){
		::GlobalFree(m_pDib);
		m_pDib=NULL;
		AfxMessageBox("Can not read from file!");
		return FALSE;
	}
	f.Close();
	for (int i=RawWidth*RawHeight-1;i>=0;i--){
		m_pDib[i*3]=m_pDib[i*3+1]=m_pDib[i*3+2]=m_pDib[i];
	}
	m_bmInfo.bmiHeader.biWidth=RawWidth;
	m_bmInfo.bmiHeader.biHeight=RawHeight;
	return TRUE;
}

BOOL CImgGrabProcDoc::OpenBmpFile(LPCTSTR lpszPathName)
//open an bitmap image and save it in m_pDib, the bitmap must be 24 bits
//2002.04.27
{
	CFile f;
	if (!f.Open(lpszPathName,CFile::modeRead)){
		AfxMessageBox("Can not open file!");
		return FALSE;
	}
	unsigned long nFileLen=f.GetLength();
	BYTE* pBmp=(BYTE*)::GlobalAlloc(GMEM_FIXED,nFileLen);
	if (!pBmp){
		AfxMessageBox("Failed to allocate memory!");
		return FALSE;
	}
	if (f.ReadHuge(pBmp,nFileLen)!=nFileLen){
		::GlobalFree(pBmp);
		AfxMessageBox("Can not read from file!");
		return FALSE;
	}
	f.Close();
	if ((((BITMAPFILEHEADER*)pBmp)->bfType)!=((WORD)('M'<< 8)|'B')){
		::GlobalFree(pBmp);
		AfxMessageBox("Not a bitmap file!");
		return FALSE;
	}
	if (((BITMAPINFOHEADER*)(pBmp+sizeof(BITMAPFILEHEADER)))->biBitCount!=24){
		::GlobalFree(pBmp);
		AfxMessageBox("Not a 24 colors bitmap file!");
		return FALSE;
	}
	if (m_pDib){
		::GlobalFree(m_pDib);
	}
	m_pDib=(BYTE*)::GlobalAlloc(GMEM_FIXED,nFileLen-sizeof(BITMAPFILEHEADER)-sizeof(BITMAPINFOHEADER));
	if (!m_pDib){
		::GlobalFree(pBmp);
		AfxMessageBox("Failed to allocate memory!");
		return FALSE;
	}
	::CopyMemory(m_pDib,pBmp+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER),nFileLen-sizeof(BITMAPFILEHEADER)-sizeof(BITMAPINFOHEADER));
	BITMAPINFOHEADER* bmih=(BITMAPINFOHEADER*)(pBmp+sizeof(BITMAPFILEHEADER));
	m_bmInfo.bmiHeader.biWidth=bmih->biWidth;
	m_bmInfo.bmiHeader.biHeight=bmih->biHeight;
	::GlobalFree(pBmp);
	return TRUE;
}
	
void CImgGrabProcDoc::OnFileSaveas256() 
//save the m_pDib as bitmap 256
//2002.04.27
{
	if (!m_pDib){
		AfxMessageBox("No image to be save!");
		return;
	}
	CFileDialog fDlg(FALSE,NULL,"*.bmp",OFN_OVERWRITEPROMPT,"bitmap file (*.bmp)|*.bmp||",NULL);
	if (fDlg.DoModal()!=IDOK){
		return;
	}
	WORD nWidth=(m_bmInfo.bmiHeader.biWidth+3)/4*4;
	WORD nWidthOri=(m_bmInfo.bmiHeader.biWidth*3+3)/4*4;
	CFile f;
	f.Open(fDlg.GetPathName(),CFile::modeCreate | CFile::modeWrite);
	BITMAPFILEHEADER bmfh;
	bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4;
	bmfh.bfReserved1=0;
	bmfh.bfReserved2=0;
	bmfh.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+256*4+nWidth*m_bmInfo.bmiHeader.biHeight;
	bmfh.bfType=(WORD)('M'<< 8)|'B';
	f.Write(&bmfh,sizeof(BITMAPFILEHEADER));
	BITMAPINFOHEADER bmih;
	bmih.biBitCount=8;
	bmih.biClrImportant=0;
	bmih.biClrUsed=0;
	bmih.biCompression=0;
	bmih.biHeight=m_bmInfo.bmiHeader.biHeight;
	bmih.biPlanes=1;
	bmih.biSize=sizeof(BITMAPINFOHEADER);
	bmih.biSizeImage=nWidth*m_bmInfo.bmiHeader.biHeight;
	bmih.biWidth=m_bmInfo.bmiHeader.biWidth;
	bmih.biXPelsPerMeter=1;
	bmih.biYPelsPerMeter=1;
	f.Write(&bmih,sizeof(BITMAPINFOHEADER));
	BYTE pal[256*4];
	for (int i=0;i<256;i++){
		pal[i*4]=pal[i*4+1]=pal[i*4+2]=i;
		pal[i*4+3]=0;
	}
	f.Write(&pal,256*4);

	/*BYTE *pDibTmp=(BYTE*)::GlobalAlloc(GMEM_FIXED,nWidth*m_bmInfo.bmiHeader.biHeight);
	for (WORD j=0;j<m_bmInfo.bmiHeader.biHeight;j++){
		for (WORD i=0;i<nWidth;i++){
			if (i<m_bmInfo.bmiHeader.biWidth){
				CString s;
				s.Format("%d:%d:%d",m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3],
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3+1],
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3+2]);
				AfxMessageBox(s);
				pDibTmp[j*nWidth+i]=(m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3]+
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3+1]+
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3+2])/3;
			}
			else{
				pDibTmp[j*nWidth+i]=0;
			}
		}
	}
	f.WriteHuge(pDibTmp,nWidth*m_bmInfo.bmiHeader.biHeight);*/

	BYTE *pDibTmp=(BYTE*)::GlobalAlloc(GMEM_FIXED,nWidth*m_bmInfo.bmiHeader.biHeight);
	if (!pDibTmp){
		AfxMessageBox("Failed to allocate memory!");
		f.Close();
		return;
	}
	for (WORD j=0,k=0;j<m_bmInfo.bmiHeader.biHeight;j++,k++){
		for (WORD i=0,l=0;i<nWidthOri-2;i+=3,l++){
			pDibTmp[k*nWidth+l]=(m_pDib[j*nWidthOri+i]+
				m_pDib[j*nWidthOri+i+1]+
				m_pDib[j*nWidthOri+i+2])/3;
		}
	}
	f.WriteHuge(pDibTmp,nWidth*m_bmInfo.bmiHeader.biHeight);

	::GlobalFree(pDibTmp);
	f.Close();
}

void CImgGrabProcDoc::OnFileSaveas24() 
//save the m_pDib as bitmap 24
//2002.05.18
{
	if (!m_pDib){
		AfxMessageBox("No image to be save!");
		return;
	}
	CFileDialog fDlg(FALSE,NULL,"*.bmp",OFN_OVERWRITEPROMPT,"bitmap file (*.bmp)|*.bmp||",NULL);
	if (fDlg.DoModal()!=IDOK){
		return;
	}
	CFile f;
	f.Open(fDlg.GetPathName(),CFile::modeCreate | CFile::modeWrite);
	BITMAPFILEHEADER bmfh;
	bmfh.bfOffBits=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	bmfh.bfReserved1=0;
	bmfh.bfReserved2=0;
	bmfh.bfSize=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+3*m_bmInfo.bmiHeader.biHeight*m_bmInfo.bmiHeader.biHeight;
	bmfh.bfType=(WORD)('M'<< 8)|'B';
	f.Write(&bmfh,sizeof(BITMAPFILEHEADER));
	BITMAPINFOHEADER bmih;
	bmih.biBitCount=24;
	bmih.biClrImportant=0;
	bmih.biClrUsed=0;
	bmih.biCompression=0;
	bmih.biHeight=m_bmInfo.bmiHeader.biHeight;
	bmih.biPlanes=1;
	bmih.biSize=sizeof(BITMAPINFOHEADER);
	bmih.biSizeImage=m_bmInfo.bmiHeader.biWidth*m_bmInfo.bmiHeader.biHeight;
	bmih.biWidth=m_bmInfo.bmiHeader.biWidth;
	bmih.biXPelsPerMeter=1;
	bmih.biYPelsPerMeter=1;
	f.Write(&bmih,sizeof(BITMAPINFOHEADER));
	f.WriteHuge(m_pDib,m_bmInfo.bmiHeader.biWidth*m_bmInfo.bmiHeader.biHeight*3);
	f.Close();
}

//********************Gauss Smooth************************
void CImgGrabProcDoc::OnGaussSmooth() 
{
	if (!m_pDib){
		AfxMessageBox("No Image Opened");
		return;
	}
	ULONG nDibSize=m_bmInfo.bmiHeader.biWidth *m_bmInfo.bmiHeader.biHeight *3;
	BYTE* pDibTmp=(BYTE*)::GlobalAlloc (GMEM_FIXED,nDibSize);
	if (!pDibTmp){
		AfxMessageBox("Fail to allocate memory");
		return;
	}
	GaussDlg dlg;//choose Causs smooth mode
	if (dlg.DoModal()==IDOK){
		BeginWaitCursor();
		::CopyMemory(pDibTmp,m_pDib,nDibSize);
		if (!dlg.m_RGB){
			//not RGB processe
			for (int j=1;j<m_bmInfo.bmiHeader.biHeight-1;j++){
				for (int i=1;i<m_bmInfo.bmiHeader.biWidth-1;i++){
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3]=	
					m_pDib[(j*m_bmInfo.bmiHeader.biWidth+i)*3+1]=

⌨️ 快捷键说明

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