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

📄 dipdemodlg.cpp

📁 阮秋琦的数字图像处理学一书所附的代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// DIPDemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DIPDemo.h"
#include "DIPDemoDlg.h"
#include "ProcessDlg.h"
#include "HistogramDlg.h"
#include "Scanner.h"
#include "CPEControl.h"
#include "Splash.h"
#include "HelpDlg.h"

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIPDemoDlg dialog

CDIPDemoDlg::CDIPDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDIPDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDIPDemoDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
	buttonptr=0;
	menufocus=-1;
	lpBitmap=0;
	lpBackup=0;
	Grabbing=FALSE;
//	lpPoints=0;
}

void CDIPDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDIPDemoDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

#define WM_BUTTONCLICK WM_USER+10000

BEGIN_MESSAGE_MAP(CDIPDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDIPDemoDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_ERASEBKGND()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_MESSAGE(WM_BUTTONCLICK,OnClick)
	ON_WM_LBUTTONDBLCLK()
	ON_WM_DESTROY()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDIPDemoDlg message handlers
#define BUTTON_LEFT1 370
#define BUTTON_LEFT2 510
#define BUTTON_LEFT3 650

enum {INPUT_FILE,INPUT_GRAB,INPUT_SNAP,INPUT_SCAN,INPUT_PASTE,INPUT_RESTORE,
NOCOLOR,NOISE_GAUSSIAN,NOISE_PEPPER,
HISTOEQ,HISTOSHOW,SMOOTH,
EDGE,PSEUDOCOLOR,BRIGHTMODIFY,BIVALUE,
GEOMETRICAL,
TRANSFORM,
OUTPUT_SAVEAS,OUTPUT_PRINT,OUTPUT_COPY
};

BOOL CDIPDemoDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	SetWindowText("数字图像处理实验演示软件");
	CSplashWnd::EnableSplashScreen();
	CSplashWnd::ShowSplashScreen(this);

	//图像输入
	AddButton(BUTTON_LEFT1,120,INPUT_FILE,"打开文件");
	AddButton(BUTTON_LEFT2,120,INPUT_GRAB,"开始采集");
	AddButton(BUTTON_LEFT3,120,INPUT_SNAP,"捕捉图像");

	AddButton(BUTTON_LEFT1,160,INPUT_PASTE,"从剪贴板粘贴");
	AddButton(BUTTON_LEFT2,160,INPUT_SCAN,"打开扫描软件");
	AddButton(BUTTON_LEFT3,160,NOCOLOR,"去除彩色");

	AddButton(BUTTON_LEFT1,200,NOISE_GAUSSIAN,"随机噪声");
	AddButton(BUTTON_LEFT2,200,NOISE_PEPPER,"椒盐噪声");
	AddButton(BUTTON_LEFT3,200,INPUT_RESTORE,"恢复原始图像");

	//图像处理
	AddButton(BUTTON_LEFT1,300,GEOMETRICAL,"几何变换");
	AddButton(BUTTON_LEFT2,300,BRIGHTMODIFY,"灰度反转");
	AddButton(BUTTON_LEFT3,300,HISTOEQ,"直方图均衡化");

	AddButton(BUTTON_LEFT1,340,HISTOSHOW,"直方图统计");
	AddButton(BUTTON_LEFT2,340,SMOOTH,"图像复原");
	AddButton(BUTTON_LEFT3,340,EDGE,"边缘提取");

	AddButton(BUTTON_LEFT1,380,PSEUDOCOLOR,"伪彩色");
	AddButton(BUTTON_LEFT2,380,BIVALUE,"二值化");
	AddButton(BUTTON_LEFT3,380,TRANSFORM,"图像变换");

	//图像输出
	AddButton(BUTTON_LEFT1,480,OUTPUT_SAVEAS,"另存为...");
	AddButton(BUTTON_LEFT2,480,OUTPUT_PRINT,"打印输出");
	AddButton(BUTTON_LEFT3,480,OUTPUT_COPY,"复制到剪贴板");

	AddButton(BUTTON_LEFT2,530,-2,"最小化");
	AddButton(BUTTON_LEFT3,530,-1,"退出系统");
	source.Create(0,"Source",WS_CHILD|WS_VISIBLE,CRect(20,20,340,260), this, 10000);
//	source.SetScroll(360,280);

	dest.Create(0,"Destination",WS_CHILD|WS_VISIBLE,CRect(20,310,340,550), this, 10001);
//	dest.SetScroll(360,280);

	ShowWindow(SW_MAXIMIZE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

#define VHIBUTTON RGB(0,0,240)
#define HIBUTTON RGB(0,0,220)
#define LOBUTTON RGB(0,0,128)
#define BUTTON RGB(0,0,160)

/*
#define VHIBUTTON RGB(240,220,160)
#define HIBUTTON RGB(220,200,150)
#define LOBUTTON RGB(128,110,70)
#define BUTTON RGB(160,140,100)
*/
/*
#define VHIBUTTON RGB(0,220,220)
#define HIBUTTON RGB(0,200,200)
#define LOBUTTON RGB(0,128,128)
#define BUTTON RGB(0,160,160)
*/
void DrawButton(CDC *pDC,int x0,int y0,int state,const char *caption)
{
	pDC->FillSolidRect(x0,y0,120,24,state?HIBUTTON:BUTTON);
	if (state==0)
		pDC->Draw3dRect(x0,y0,120,24,HIBUTTON,LOBUTTON);
	else if (state==1)
		pDC->Draw3dRect(x0,y0,120,24,VHIBUTTON,LOBUTTON);
	else
		pDC->Draw3dRect(x0,y0,120,24,BUTTON,BUTTON);
	
	CSize sz=pDC->GetTextExtent(caption);
	pDC->SetBkColor(state?HIBUTTON:BUTTON);
	pDC->SetTextColor(state?RGB(255,255,255):RGB(220,220,240));
	if (state==2)
		pDC->TextOut(x0+61-sz.cx/2,y0+4,caption);
	else
		pDC->TextOut(x0+60-sz.cx/2,y0+3,caption);
}

void CDIPDemoDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDIPDemoDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CPaintDC dc(this);
		int i;
		for(i=0;i<buttonptr;i++)
		{
			DrawButton(&dc,buttonx[i],buttony[i],0,buttoncaption[i]);
		}
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDIPDemoDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}
/*
#define HIYELLOW RGB(230,200,120)
#define YELLOW RGB(180,160,100)
#define LOYELLOW RGB(120,100,70)
*/
#define HIYELLOW RGB(160,160,160)
#define LOYELLOW RGB(80,80,80)
#define YELLOW RGB(128,128,128)

void DrawFrame(CDC *pDC, int x0,int y0,int cx,int cy)
{
	pDC->FillSolidRect(x0+1,y0+1,cx+20,cy+20,YELLOW);
	pDC->Draw3dRect(x0,y0,cx+22,cy+22,HIYELLOW,LOYELLOW);
	pDC->Draw3dRect(x0+9,y0+9,cx+2,cy+2,LOYELLOW,HIYELLOW);
//	pDC->FillSolidRect(x0+11,y0+11,cx,cy,RGB(160,140,120));
}

#define HIBORDER RGB(200,80,240)
#define BORDER RGB(160,60,192)
#define LOBORDER RGB(120,40,160)

void DrawBorder(CDC *pDC, int x0,int y0,int cx,int cy,const char *caption)
{
	pDC->Draw3dRect(x0,y0,cx,cy,HIBORDER,LOBORDER);
	pDC->Draw3dRect(x0+1,y0+1,cx-2,cy-2,LOBORDER,HIBORDER);
	pDC->FillSolidRect(x0+25,y0-18,100,19,BORDER);
	pDC->Draw3dRect(x0+24,y0-19,102,21,HIBORDER,LOBORDER);
	pDC->SetBkColor(BORDER);
	CSize sz=pDC->GetTextExtent(caption);
	pDC->TextOut(x0+76-sz.cx/2,y0-17,caption);
}

BOOL CDIPDemoDlg::OnEraseBkgnd(CDC* pDC) 
{
	// TODO: Add your message handler code here and/or call default
	CBitmap bm;
	BITMAP bmi;
	bm.LoadBitmap(IDB_BACKGROUND);
	bm.GetBitmap(&bmi);
	CDC memdc;
	memdc.CreateCompatibleDC(pDC);
	memdc.SelectObject(bm);
	int x,y;
	for(y=0;y<6;y++)
	{
		for(x=0;x<8;x++)
		{
			pDC->BitBlt(x*bmi.bmWidth,y*bmi.bmHeight,bmi.bmWidth,bmi.bmHeight,&memdc,0,0,SRCCOPY);
		}
	}
	bm.DeleteObject();
	bm.LoadBitmap(IDB_FRAME);
	bm.GetBitmap(&bmi);
	memdc.SelectObject(bm);

	pDC->BitBlt(10,10,340,260,&memdc,0,0,SRCCOPY);
	pDC->BitBlt(10,300,340,260,&memdc,0,0,SRCCOPY);

	bm.DeleteObject();
//	DrawFrame(pDC,10,10,320,240);
//	DrawFrame(pDC,10,300,320,240);
	pDC->SetTextColor(RGB(240,240,240));
//	pDC->SetBkMode(TRANSPARENT);
	DrawBorder(pDC,BUTTON_LEFT1-10,105,420,134,"图像输入");
	DrawBorder(pDC,BUTTON_LEFT1-10,285,420,134,"图像处理");
	DrawBorder(pDC,BUTTON_LEFT1-10,465,420,54,"图像输出");

	bm.LoadBitmap(IDB_TITLE);
	bm.GetBitmap(&bmi);
	memdc.SelectObject(bm);
	pDC->BitBlt(370,20,400,41,&memdc,0,0,SRCCOPY);
	bm.DeleteObject();

	return TRUE;
}

int CDIPDemoDlg::HitTestButton(int x0, int y0)
{
	int i;
	for(i=0;i<buttonptr;i++)
	{
		if (x0>=buttonx[i] && y0>=buttony[i] && x0<=buttonx[i]+120 && y0<=buttony[i]+24) break;
	}
	if (i<buttonptr) return i;
	return -1;
}

void CDIPDemoDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int focused=FALSE;
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus!=pos)
		{
			MenuButton(pos,1);
			if (menufocus!=-1) MenuButton(menufocus,0);
			menufocus=pos;
		}
		focused=TRUE;
	}
	if (!focused && menufocus!=-1)
	{
		MenuButton(menufocus,0);
		menufocus=-1;
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CDIPDemoDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus!=pos)
		{
			if (menufocus!=-1) MenuButton(menufocus,0);
			menufocus=pos;
		}
		MenuButton(pos,2);
		menuclick=pos;
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CDIPDemoDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int pos=HitTestButton(point.x,point.y);
	if (pos!=-1)
	{
		if (menufocus==pos && menuclick==pos)
		{
			MenuButton(pos,1);
			PostMessage(WM_BUTTONCLICK,pos,0);
		}
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CDIPDemoDlg::AddButton(int x,int y,int ID, const char *caption)
{
	buttonx[buttonptr]=x;
	buttony[buttonptr]=y;
	strcpy(buttoncaption[buttonptr],caption);
	buttonid[buttonptr]=ID;
	MenuButton(buttonptr,0);
	buttonptr++;
}

void CDIPDemoDlg::MenuButton(int pos,int status)
{
	CClientDC dc(this);
	DrawButton(&dc,buttonx[pos],buttony[pos],status,buttoncaption[pos]);
}

void CDIPDemoDlg::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	CDIPDemoDlg::OnLButtonDown(nFlags,point);
	CDialog::OnLButtonDblClk(nFlags, point);
}

void CDIPDemoDlg::LoadBitmap()
{
	BITMAPINFOHEADER *pInfo;
	pInfo=(BITMAPINFOHEADER *)(lpBitmap+sizeof(BITMAPFILEHEADER));
	nWidth=pInfo->biWidth;
	nByteWidth=nWidth*3;
	if (nByteWidth%4) nByteWidth+=4-(nByteWidth%4);
	nHeight=pInfo->biHeight;
	if (pInfo->biBitCount!=24)
	{
		if (pInfo->biBitCount!=8)
		{
			AfxMessageBox("无效位图");
			delete lpBitmap;
			lpBitmap=0;
			return;
		}
		unsigned int PaletteSize=1<<pInfo->biBitCount;
		if (pInfo->biClrUsed!=0 && pInfo->biClrUsed<PaletteSize) PaletteSize=pInfo->biClrUsed;
		lpBits=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
		RGBQUAD *pPalette=(RGBQUAD *)lpBits;
		lpBits+=sizeof(RGBQUAD)*PaletteSize;
		nLen=sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER)+nByteWidth*nHeight;
		BYTE *lpTemp=lpBitmap;
		lpBitmap=new BYTE[nLen];
		BITMAPFILEHEADER bmh;
		BITMAPINFOHEADER bmi;
		bmh.bfType='B'+'M'*256;
		bmh.bfSize=nLen;
		bmh.bfReserved1=0;
		bmh.bfReserved2=0;
		bmh.bfOffBits=54;
		bmi.biSize=sizeof(BITMAPINFOHEADER);
		bmi.biWidth=nWidth;
		bmi.biHeight=nHeight;
		bmi.biPlanes=1;
		bmi.biBitCount=24;
		bmi.biCompression=BI_RGB;
		bmi.biSizeImage=0;
		bmi.biXPelsPerMeter=0;
		bmi.biYPelsPerMeter=0;
		bmi.biClrUsed=0;
		bmi.biClrImportant=0;
		int nBWidth=pInfo->biWidth;
		if (nBWidth%4) nBWidth+=4-(nBWidth%4);
		memset(lpBitmap,0,nLen);
		memcpy(lpBitmap,&bmh,sizeof(BITMAPFILEHEADER));
		memcpy(lpBitmap+sizeof(BITMAPFILEHEADER),&bmi,sizeof(BITMAPINFOHEADER));
		BYTE *lpBits2=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
		int x,y,p1,p2,Palette;
		for(y=0;y<nHeight;y++)
		{
			for(x=0;x<nWidth;x++)
			{
				p1=y*nBWidth+x;
				p2=y*nByteWidth+x*3;
				if (lpBits[p1]<PaletteSize) Palette=lpBits[p1];
				else Palette=0;
				lpBits2[p2]=pPalette[Palette].rgbBlue;
				lpBits2[p2+1]=pPalette[Palette].rgbGreen;
				lpBits2[p2+2]=pPalette[Palette].rgbRed;
			}
		}
		delete lpTemp;
	}
	lpBits=lpBitmap+sizeof(BITMAPFILEHEADER)+sizeof(BITMAPINFOHEADER);
	if (lpBackup) delete lpBackup;
	lpBackup=new BYTE[nLen];
	memcpy(lpBackup,lpBitmap,nLen);
}

void CDIPDemoDlg::OpenFile() 
{
	// TODO: Add your control notification handler code here
	CFile File=0;
	CFileDialog dlg(TRUE,0,0,OFN_HIDEREADONLY,"位图文件|*.bmp|所有文件|*.*||",this);
	if (dlg.DoModal()==IDOK)
	{
		FileName=dlg.GetPathName();
		if (!File.Open(FileName,CFile::modeRead)) return;
		// TODO: add loading code here
		if (lpBitmap) delete lpBitmap;
		nLen=File.GetLength();
		lpBitmap=new BYTE[nLen];
		File.Read(lpBitmap,nLen);
		LoadBitmap();
		if (lpBitmap) source.SetImage(nWidth,nHeight,lpBits);
	}
}

void CDIPDemoDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
	if (lpBitmap) delete lpBitmap;
	if (lpBackup) delete lpBackup;
//	if (lpPoints) delete lpPoints;
}

void HorzMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void VertMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));
void CornerMirror(int nWidth,int nHeight,BYTE *lpInput,BYTE *lpOutput,void (* Progress)(int Pos));

void CDIPDemoDlg::Geometrical()
{
	if (lpBitmap)
	{
		CProcessDlg dlg;
		dlg.Introduce="几何变换";
		dlg.Process1=HorzMirror;
		dlg.Option1="水平镜像";
		dlg.Process2=VertMirror;
		dlg.Option2="垂直镜像";
		dlg.Process3=CornerMirror;
		dlg.Option3="对角镜像";
		dlg.pDest=&dest;
		dlg.lpInput=lpBits;
		dlg.nWidth=nWidth;
		dlg.nHeight=nHeight;
		dlg.DoModal();
	}
}

⌨️ 快捷键说明

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