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

📄 huffencodedialog.cpp

📁 熵编码的源程序
💻 CPP
字号:
// HuffencodeDialog.cpp : implementation file
//
#include "stdafx.h"
#include "entropyCoding.h"
#include "HuffencodeDialog.h"
#include "huff_en.h"
#include "huff_de.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CHuffencodeDialog dialog

CHuffencodeDialog::CHuffencodeDialog(CWnd* pParent /*=NULL*/)
	: CDialog(CHuffencodeDialog::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHuffencodeDialog)
	m_infile = _T("");
	m_Outfile = _T("");
	//}}AFX_DATA_INIT
}
void CHuffencodeDialog::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHuffencodeDialog)
	DDX_Text(pDX, IDC_Infile, m_infile);
	DDX_Text(pDX, IDC_Outfile, m_Outfile);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHuffencodeDialog, CDialog)
	//{{AFX_MSG_MAP(CHuffencodeDialog)
	ON_BN_CLICKED(IDC_OPEN1, OnOpen1)
	ON_BN_CLICKED(IDC_OPEN2, OnOpen2)
	ON_BN_CLICKED(IDC_OK_De, OnOKDe)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHuffencodeDialog message handlers

void CHuffencodeDialog::OnOpen1() 
{
	UpdateData(true);
    static char szFilter[] = "raw(*.raw)|*.raw|huf(*.huf)|*.huf|acc(*.acc)|*.acc|run(*.run)|*.run|";
	CFileDialog FileDlg( TRUE, NULL,NULL,OFN_HIDEREADONLY, szFilter);
    if( FileDlg.DoModal() == IDOK ) 
	{
		m_szFilePathName1=FileDlg.GetPathName();
  	}
	else
	{  
		 m_szFilePathName1 = "F:\\standard_pictures\\MISSUSA_raw\\lena.raw";
	}
	m_infile=m_szFilePathName1;
	m_Outfile=m_infile.Left(m_infile.GetLength()-4);
	if(m_infile.Right(3)=="raw")
		m_Outfile=m_Outfile+".huf";
	else
		m_Outfile=m_Outfile+"_De.raw";
    UpdateData(false);
}

void CHuffencodeDialog::OnOpen2() 
{
	UpdateData(true);
    static char szFilter[] = "ALL Files(*.*)|*.*|";
	CFileDialog FileDlg( TRUE, NULL,NULL,OFN_HIDEREADONLY, szFilter);
    if( FileDlg.DoModal() == IDOK ) 
	{
		m_szFilePathName2=FileDlg.GetPathName();
  	}
	else
	{  
		 m_szFilePathName2 = "F:\\standard_pictures\\MISSUSA_raw\\lena.raw";
	}
	m_Outfile=m_szFilePathName2;
    UpdateData(false);
}

void CHuffencodeDialog::OnOK() 
{
      if ((ifile = fopen (m_infile, "rb")) != NULL)
      {
         fseek (ifile, 0L, 2);
         file_size = (unsigned long) ftell (ifile);
         fseek (ifile, 0L, 0);
         get_frequency_count ();
         build_initial_heap ();
         build_code_tree ();
         if (!generate_code_table ())
            AfxMessageBox ("ERROR!  Code Value Out of Range. Cannot Compress.\n");
         else
         {
            if ((ofile = fopen (m_Outfile, "wb")) != NULL)
            {
               fwrite (&file_size, sizeof (file_size), 1, ofile);
               fwrite (code, 2, 256, ofile);
               fwrite (code_length, 1, 256, ofile);

               fseek (ifile, 0L, 0);
               compress_image ();

               fclose (ofile);
            }            
         }
         fclose (ifile);
      }
	  CString info;
	  info.Format("霍夫曼编码结束!输出文件:%s\n输入",m_Outfile);//second
      AfxMessageBox(info);
}

void CHuffencodeDialog::OnOKDe() 
{
	if ((ifile = fopen (m_infile, "rb")) != NULL)
      {
         fread (&file_size, sizeof (file_size), 1, ifile);
         fread (code, 2, 256, ifile);
         fread (code_length, 1, 256, ifile);
         build_decomp_tree ();
         if ((ofile = fopen (m_Outfile, "wb")) != NULL)
         {
            decompress_image();
            fclose (ofile);
         }
         fclose (ifile);
      }
	 CString info;
     info.Format("霍夫曼解码结束!输出文件:%s",m_Outfile);//second
     AfxMessageBox(info);
}

⌨️ 快捷键说明

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