www.pudn.com > 图像识别.rar > DlgArith.cpp
///////////////////////////////////////////////////////////////////////////// // CDlgArith dialog #include "stdafx.h" #include "ImageProcessing.h" #include "DlgCoding.h" #include#ifdef _DEBUG #define new DEBUG_NEW #undef THIS_FILE static char THIS_FILE[] = __FILE__; #endif #define fPro4Zero 0.25; #define fPro4One 0.75; CDlgArith::CDlgArith(CWnd* pParent /*=NULL*/) : CDialog(CDlgArith::IDD, pParent) { //{{AFX_DATA_INIT(CDlgArith) m_ArithSerial = _T(""); m_ArithOutput = _T(""); m_ArithDecode = _T(""); //}}AFX_DATA_INIT } void CDlgArith::DoDataExchange(CDataExchange* pDX) { CDialog::DoDataExchange(pDX); //{{AFX_DATA_MAP(CDlgArith) DDX_Control(pDX, IDC_EDIT1, m_ConArithSer); DDX_Control(pDX, IDCODING, m_coding); DDX_Control(pDX, IDDECODING, m_decoding); DDX_Text(pDX, IDC_EDIT1, m_ArithSerial); DDV_MaxChars(pDX, m_ArithSerial, 15); DDX_Text(pDX, IDC_EDIT2, m_ArithOutput); DDX_Text(pDX, IDC_EDIT4, m_ArithDecode); //}}AFX_DATA_MAP } BEGIN_MESSAGE_MAP(CDlgArith, CDialog) //{{AFX_MSG_MAP(CDlgArith) ON_BN_CLICKED(IDDECODING, OnDecoding) ON_BN_CLICKED(IDCODING, OnCoding) //}}AFX_MSG_MAP END_MESSAGE_MAP() ///////////////////////////////////////////////////////////////////////////// // CDlgArith message handlers //void CDlgArith::OnOK() //{ // CDialog::OnOK(); //} /////////////////////////////////////////////////////////////// // DlgCodingIArith dialog /*********************************************************** 实现对已经编码的码字进行解码的功能 ********************************************************* */ void CDlgArith::OnDecoding() { // 二值序列的长度 int nOutLength; // 算术编码的长度 int nInLength; // 编码区间的上限和下限 double dHigh=1.0; double dLow=0.0; // 编码区间的长度 double dRange=1.0; // 判断二值序列是否全零 int nNo1=0; // 循环变量 int i; // 二进制表示为十进制 double dTenCode=0; // 中间变量 double d2Pow; double dTemp; // 接收数据 UpdateData(TRUE); // 解码显示清空 m_ArithDecode = _T(""); // 显示数据 UpdateData(FALSE); // 算术编码的长度 nInLength =m_ArithOutput.GetLength(); // 将二进制序列转化为十进制,并判断是否为零 for (i=0; i dTenCode) { // 输出0 m_ArithDecode=m_ArithDecode+'0'; // 编码区间上下限的计算 dLow=dLow; dHigh=dLow+dRange*fPro4Zero; // 区间范围 dRange=dHigh-dLow; } else { // 输出1 m_ArithDecode=m_ArithDecode+'1'; // 编码区间上下限的计算 dLow=dLow+dRange*fPro4Zero; dHigh=dHigh; // 区间范围 dRange=dHigh-dLow; } } } else { for(i=0;i = d2Pow) { m_ArithOutput = m_ArithOutput + "1"; dTemp = dTemp - d2Pow; } else m_ArithOutput = m_ArithOutput + "0"; } // 转化后是否有余数 if(dTemp > 0) { // 二进制小数进行进位 for(i = nOutLength-1; i >= 0; i--) { // 进位,1转化为0 if(m_ArithOutput.Mid(i,1) == '1') { m_ArithOutput.Delete(i,1); m_ArithOutput.Insert(i,"0"); } // 进位完成,最后的0位转化为1 else { m_ArithOutput.Delete(i,1); m_ArithOutput.Insert(i,"1"); break; } } } // 编码完成,数据更新 UpdateData(FALSE); // 允许进行解码 m_decoding.EnableWindow(TRUE); // 解码前禁止编码 m_coding.EnableWindow(FALSE); // 解码前禁止输入新的二进制序列 m_ConArithSer.EnableWindow(FALSE); }