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

📄 metrixdlg.cpp

📁 该软件和代码实现的功能是对图象的加密和解密,运行环境是Xp+matlab,代码中有说明,很容易看明白.
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// MetrixDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Metrix.h"
#include "MetrixDlg.h"
#include "KeyDlg.h"
#include "iostream.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
extern CString G_strPass;
/////////////////////////////////////////////////////////////////////////////
// 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()

/////////////////////////////////////////////////////////////////////////////
// CMetrixDlg dialog

CMetrixDlg::CMetrixDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CMetrixDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CMetrixDlg)
    
		// 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);
}

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

BEGIN_MESSAGE_MAP(CMetrixDlg, CDialog)
	//{{AFX_MSG_MAP(CMetrixDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
	ON_WM_DESTROY()
	ON_BN_CLICKED(IDC_SHOW_OLD, OnShowOld)
	ON_BN_CLICKED(IDC_OPEN_IMG, OnOpenImg)
	ON_BN_CLICKED(IDC_OLD_ZHIFANG, OnOldZhifang)
	ON_BN_CLICKED(IDC_SHOW_NEW, OnShowNew)
	ON_BN_CLICKED(IDC_NEW_ZHIFANG, OnNewZhifang)
	ON_BN_CLICKED(IDC_COMPARE, OnCompare)
	ON_BN_CLICKED(IDC_JM_OPEN, OnJmOpen)
	ON_BN_CLICKED(IDC_JM_COMPARE, OnJmCompare)
	ON_BN_CLICKED(IDC_JM_OLD, OnJmOld)
	ON_BN_CLICKED(IDC_JM_ZHIFANG, OnJmZhifang)
	ON_BN_CLICKED(IDC_JMH_NEW, OnJmhNew)
	ON_BN_CLICKED(IDC_JMH_ZHIFANG, OnJmhZhifang)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMetrixDlg message handlers

BOOL CMetrixDlg::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
	/////////////////////////

	/////////////////////////////////////////
	if(!(ep=engOpen(NULL)))
	{
		AfxMessageBox("Can't start MATLAB engine");
		return 0;
	}
	else 
        AfxMessageBox("start MATLAB engine");
return TRUE;  // return TRUE  unless you set the focus to a control
}

void CMetrixDlg::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 CMetrixDlg::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
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CMetrixDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CMetrixDlg::OnButtonOpen() 
{
	// TODO: Add your control notification handler code here
    
	TCHAR pszBuffer[_MAX_PATH];
	BROWSEINFO bi; 	
    LPITEMIDLIST pidl;
	bi.hwndOwner = NULL;
	bi.pidlRoot = NULL;
	bi.pszDisplayName = pszBuffer;
	bi.lpszTitle = _T("数据文件存放路径");
	bi.ulFlags = BIF_RETURNFSANCESTORS | BIF_RETURNONLYFSDIRS;
	bi.lpfn = NULL;
	bi.lParam = 0;
	if((pidl = SHBrowseForFolder(&bi)) != NULL)
	{
		if(SHGetPathFromIDList(pidl, pszBuffer))
		{
			CString strTemp(pszBuffer);   mat_Path = strTemp;
			if (mat_Path.GetLength() <= 1) 
			{
			}
			else if (mat_Path.Right(1) != _T("\\")) 	
				mat_Path += _T("\\");
		}
	}
	GetDlgItem(IDC_STATIC_PATH)->SetWindowText(mat_Path);
   // AfxMessageBox(mat_Path);
    if(mat_Path=="")
       AfxMessageBox("please choose the path to save mat file");

		
	    
	
/*	CFileDialog  Dlg(TRUE,_T("*.mat"), "",NULL,
    "Data Files(*.mat)|*.mat*|All Files (*.*)|*.*||");
	*/
}



void CMetrixDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	AfxMessageBox("cloase MATLAB engine");
    engClose(ep);
// TODO: Add your message handler 
}



void CMetrixDlg::OnShowOld() 
{
	// TODO: Add your control notification handler code here
   //  CString str_tem;
   //img_length=img_path_name.GetLength();
   //////////////////////////////////////////
   //str_tem=path;//字符串c格式和string之间转化path[]
   //////////////////////////////////////////////////
   // AfxMessageBox(img_path_name);
    if(img_path_name=="")
	{  
		AfxMessageBox("please choose tif file");
	    return;
	   }
	else if(mat_Path=="")
	{
		AfxMessageBox("please choose path to save mat file");
	    return;
	   }
	if(flag_run!=1)
		 return;
    A_mx=mxCreateString(img_path_name);
	B_mx=mxCreateString(mat_Path_Name);
   //AfxMessageBox("hahhahh11111111");
	engPutVariable(ep,"IMG_PATH_NAME",A_mx);
    engPutVariable(ep,"MAT_PATH_NAME",B_mx);
    engEvalString(ep,"OLDM=imread(IMG_PATH_NAME);");
    engEvalString(ep,"imshow(OLDM);");
    engEvalString(ep,"save(MAT_PATH_NAME);");
   //AfxMessageBox("haha2222222222");
	mxDestroyArray(A_mx);
    mxDestroyArray(B_mx);
    flag_run++;

}

void CMetrixDlg::OnOpenImg() 
{
	// TODO: Add your control notification handler code here
	CString str_tem;
	flag_run=0;
	CFileDialog img_Dlg(TRUE,_T("*.tif"), "",NULL,
    "Data Files(*.tif)|*.tif*|All Files (*.*)|*.*||");
	AfxMessageBox("请选择一个像素不超过500*500的tif文件");
	img_Dlg.DoModal();
	////
	img_path_name=img_Dlg.GetPathName();
	img_filename=img_Dlg.GetFileName();
    img_file_extern=img_Dlg.GetFileExt();
	
	if(mat_Path=="")
	{  
	   AfxMessageBox("please choose path to save mat file");
	   return;
	   }
	
    if(img_path_name=="")
	{ AfxMessageBox("please open file again");
	  return;
	  }
     else if(img_file_extern!="tif")
   {
	  AfxMessageBox("this file name is not *.tif file");
	  return;
   }
	
	CString str_yy="new";
	str_yy+=img_filename;
	
    img_save_path=mat_Path+str_yy;
	AfxMessageBox(img_save_path);
    mat_Path_Name=mat_Path+"data.mat";
	
	AfxMessageBox(mat_Path_Name);
	mfp=matOpen(mat_Path_Name,"w");
    if(mfp==NULL)
	   { AfxMessageBox("Create MAT File ERROR"); 
	    return;
		}
	flag_run++;
    
}

void CMetrixDlg::OnOldZhifang() 
{
	// TODO: Add your control notification handler code her
	if(img_path_name=="")
	{  
		AfxMessageBox("please choose tif file");
	    return;
	   }
	else if(mat_Path=="")
	{
		AfxMessageBox("please choose path to save mat file");
	    return;
	   }
	if(flag_run!=2)
		return;
	else
	engEvalString(ep,"imhist(OLDM);");
	flag_run++;
}

void CMetrixDlg::OnShowNew() 
{
	// TODO: Add your control notification handler code here
    unsigned char *A_ch,*B_ch;
	int dims[2]={0};
    long int key;
	CString str_tem;
	if(img_path_name=="")
	{ AfxMessageBox("please choose tif file");
	   return;
	   }
	if(mat_Path=="")
	{  
	   AfxMessageBox("please choose path to save mat file");
	   return;
	   }
	CKeyDlg m_KeyDlg;
     if(m_KeyDlg.DoModal()!=IDOK)
		return;
  //  AfxMessageBox(G_strPass);
    key=StringTOInt(G_strPass);
	str_tem.Format("the key is %d",key);
    AfxMessageBox(str_tem);
	if(flag_run!=3)
	     return;
	if(mfp!=NULL)
		matClose(mfp);
	mfp=matOpen(mat_Path_Name,"u");
	if(mfp==NULL)
	{AfxMessageBox("open mat file error\n");
	   return;
	   }
    A_ptr=matGetArray(mfp,"OLDM");
    M=mxGetM(A_ptr);
	N=mxGetN(A_ptr);
	A_ch=( unsigned char*)mxGetPr(A_ptr);
	//B_ch=( unsigned char*)mxGetPi(A_ptr);
	if(mxIsComplex(A_ptr))
         AfxMessageBox("this is complex\n");
//////////计算原来图像的平均相邻灰度差//////////////////////
EGD=0.0;
EGD=JM_EGD_Computer(A_ch,M,N);
/////////////////////////置乱矩阵
if(!JM_EGD_Matrix(A_ch,M,N,key))
   AfxMessageBox("Error in EGD the Matrix,this is bab Error");
//	for(i=0;i<M;i++)
	///	 for(j=0;j<N;j++)
		//	A_ch[i+j*M]^=(key+i*j)%256;
     dims[0]=M;
	 dims[1]=N;
     B_ptr=mxCreateNumericArray(2,dims,mxUINT8_CLASS,mxREAL);
     mxSetName(B_ptr,"NEWM");
	 B_ch=(unsigned char*)mxGetPr(B_ptr);
	 //
	 for(i=0;i<M;i++)
           for(j=0;j<N;j++)
		     B_ch[i+j*M]=A_ch[i+j*M];
    //
EGD_NEW=0.0;
EGD_NEW=JM_EGD_Computer(B_ch,M,N);
/////////////////////////////将新的矩阵存到mat文件中
	matPutArray(mfp,B_ptr);
    AfxMessageBox("新的矩阵数据存入文件");
///////////////////////////这里很重要,打开的文件要先关闭,不然没有办法用
if(mfp!=NULL)
	   matClose(mfp);
///////////////////////////函数LOAD打开文件并且导出变量:--)^_^..
    
//显示置乱后的图像
//	AfxMessageBox("yyyyy11111111");
	C_mx=mxCreateString(mat_Path);
    D_mx=mxCreateString(img_save_path);
	engPutVariable(ep,"MAT_PATH",C_mx);
	engPutVariable(ep,"IMG_SAVE_PATH",D_mx);
    engEvalString(ep,"addpath(MAT_PATH);");
    engEvalString(ep,"load data;");
    engEvalString(ep,"imshow(NEWM);");
	engEvalString(ep,"imwrite(NEWM,IMG_SAVE_PATH);");
//	engOutputBuffer(ep,buffer,300);
//	engEvalString(ep,"whos");
//	MessageBox((LPSTR)buffer,(LPSTR)"MATLAB-whos",MB_OK);
 // AfxMessageBox("yyyy222222222");
	
    mxDestroyArray(A_ptr);
	mxDestroyArray(B_ptr);
	mxDestroyArray(C_mx);
    mxDestroyArray(D_mx);
	flag_run++;
}

int CMetrixDlg::Josephus( unsigned int a[],int length, int start, int no)
{
     
	 int i,j;
     struct node *first,*Ahead,*pro,*pnew,*pcurrt,*pp,*pdel; 
	 first=new  node;
	 first->num=a[0];
	 Ahead=first;
     for(i=1;i<length;i++)            //指针first首先指向头结点(有数据),并且往后移动
	 {                                //构造一个循环单向链表
		  pnew=new node;
		  pnew->num=a[i];
		  first->next=pnew;
		  first=pnew;
	  }
      pnew->next=Ahead;
      pcurrt=Ahead;
	 /* cout<<"原来序列是"<<endl;
      do
	  {
		  cout<<pcurrt->num<<"---";
		  pcurrt=pcurrt->next;
	  }
	 while(pcurrt!=Ahead); 
	 */
	 pp=Ahead;                 //移动指针到start的位置
     for(i=0;i<start-1;i++)     //移动start-1个位
	 {
		 pp=pp->next;
	 }
	//  cout<<start<<"的位置数据是"<<pp->num<<endl;
	 j=0;
	 pcurrt=pp;
     while(j<length)
	 {
		 for(i=0;i<no-1;i++)
		 {   pro=pcurrt;
			 pcurrt=pcurrt->next;
		 }
		  pro->next=pcurrt->next;
		  a[j]=pcurrt->num;

		//  cout<<"pro所指结点的数据是"<<pro->num<<endl;
		  pdel=pcurrt;
	      pcurrt=pcurrt->next;//删除结点
          j++;
	      delete pdel;  //释放内存
	 }
     
return 1;
}

void CMetrixDlg::OnNewZhifang() 
{
	// TODO: Add your control notification handler code here
	if(img_path_name=="")
	{  
		AfxMessageBox("please choose tif file");
	    return;
	   }
	else if(mat_Path=="")
	{
		AfxMessageBox("please choose path to save mat file");
	    return;
	   }
	if(flag_run!=4)
		return;
	else
	engEvalString(ep,"imhist(NEWM);");
    engEvalString(ep,"clear;");
	flag_run++;
}

void CMetrixDlg::OnCompare() 
{
	// TODO: Add your control notification handler code here
	CString str_old,str_new;
	if(img_path_name=="")
	{  
		AfxMessageBox("please choose tif file");
	    return;
	   }
	else if(mat_Path=="")
	{
		AfxMessageBox("please choose path to save mat file");
	    return;
	   }
	if(flag_run!=5)
		return;

	str_old.Format("原来图像的平均相邻置乱度差为%f--运行标记是%d\n",EGD,flag_run);
    str_new.Format("新的图像的平均相邻置乱度差为%f\n",EGD_NEW);
    str_old+=str_new;
    AfxMessageBox(str_old);
	
   flag_run++;
	
}

⌨️ 快捷键说明

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