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

📄 hyperspectraldatacompressdlg.cpp

📁 改进的JPEG-LS算法
💻 CPP
📖 第 1 页 / 共 3 页
字号:
// HyperspectralDataCompressDlg.cpp : implementation file
//

#include "stdafx.h"
#include <math.h>
#include ".\AdvanceJPEG-LS\AdvanceJPEG-LSCode.h"
#include "HyperspectralImagCode.h"
#include "HyperspectralDataCompress.h"
#include "SmallEdit.h"
#include "HyperspectralDataCompressDlg.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()

/////////////////////////////////////////////////////////////////////////////
// CHyperspectralDataCompressDlg dialog

CHyperspectralDataCompressDlg::CHyperspectralDataCompressDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CHyperspectralDataCompressDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CHyperspectralDataCompressDlg)
	m_AuxDataWidth = 0;
	m_BandNumber = 0;
	m_BitsPerPixel = 0;
	m_BytesPerPixel = 0;
	m_ComperssFileName = _T("");
	m_CompressRateString = _T("");
	m_ImageHeight = 0;
	m_ImageWidth = 0;
	m_InputFileName = _T("");
	m_LinesPerBlock = 0;
	m_MaxErrString = _T("");
	m_NoticeString = _T("");
	m_PSNRString = _T("");
	m_RateString = _T("");
	m_NoticeNumberString = _T("");
	FileNumber=0;
	m_AllowMaxErrorString = _T("");
	//}}AFX_DATA_INIT
	AllowMaxErrBgn=AllowMaxErrEnd=0;
	AllowMaxErrStep=1;
	PureImageFormat=FALSE;
	CompressThreadRun=FALSE;
	m_pCompressThread=NULL;
	TestRelativeThreadRun=FALSE;
	m_pTestRelativeThread=NULL;
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CHyperspectralDataCompressDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CHyperspectralDataCompressDlg)
	DDX_Control(pDX, IDC_COMPRESS_RELATIVE_LIST, m_RelativeList);
	DDX_Text(pDX, IDC_AUX_DATA_WIDTH, m_AuxDataWidth);
	DDX_Text(pDX, IDC_BAND_NUMBER, m_BandNumber);
	DDX_Text(pDX, IDC_BITS_PER_PIXEL, m_BitsPerPixel);
	DDX_Text(pDX, IDC_BYTES_PER_PIXEL, m_BytesPerPixel);
	DDX_Text(pDX, IDC_COMPRESS_FILE_NAME, m_ComperssFileName);
	DDX_Text(pDX, IDC_COMPRESS_RATE_STATIC, m_CompressRateString);
	DDX_Text(pDX, IDC_IMAGE_HEIGHT, m_ImageHeight);
	DDX_Text(pDX, IDC_IMAGE_WIDTH, m_ImageWidth);
	DDX_Text(pDX, IDC_INPUT_FILE_NAME, m_InputFileName);
	DDX_Text(pDX, IDC_LINES_PER_BLOCK, m_LinesPerBlock);
	DDX_Text(pDX, IDC_MAX_ERROR_STATIC, m_MaxErrString);
	DDX_Text(pDX, IDC_NOTICE_STATIC, m_NoticeString);
	DDX_Text(pDX, IDC_PSNR_STATIC, m_PSNRString);
	DDX_Text(pDX, IDC_RATE_STATIC, m_RateString);
	DDX_Text(pDX, IDC_NOTICE_NUMBER_STATIC, m_NoticeNumberString);
	DDX_Text(pDX, IDC_ALLOW_MAS_ERROR, m_AllowMaxErrorString);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CHyperspectralDataCompressDlg, CDialog)
	ON_MESSAGE(WM_USER_SMALL_EDIT, OnSmallEditRecvMessage)
	ON_MESSAGE(WM_USER_HYPERSPECTRAL_COMPRESS_DLG, OnHyperspectralDataCompressRecvMessage)
	//{{AFX_MSG_MAP(CHyperspectralDataCompressDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_DESTROY()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_CLOSE()
	ON_BN_CLICKED(IDC_COMPRESS_CANCEL, OnCompressCancel)
	ON_EN_KILLFOCUS(IDC_COMPRESS_FILE_NAME, OnKillfocusCompressFileName)
	ON_BN_CLICKED(IDC_COMPRESS_OK, OnCompressOk)
	ON_BN_CLICKED(IDC_MULBAND_IMAGE_FILENAME_FIND_BUTTON, OnMulbandImageFilenameFindButton)
	ON_BN_CLICKED(IDC_OMIS_I_PREDICT_BAND, OnOmisIPredictBand)
	ON_BN_CLICKED(IDC_OMIS_II_PREDICT_BAND, OnOmisIiPredictBand)
	ON_BN_CLICKED(IDC_TEST_PREDICT_BAND, OnTestPredictBand)
	ON_EN_KILLFOCUS(IDC_AUX_DATA_WIDTH, OnKillfocusAuxDataWidth)
	ON_EN_KILLFOCUS(IDC_LINES_PER_BLOCK, OnKillfocusLinesPerBlock)
	ON_NOTIFY(NM_DBLCLK, IDC_COMPRESS_RELATIVE_LIST, OnDblclkCompressRelativeList)
	ON_EN_KILLFOCUS(IDC_IMAGE_WIDTH, OnKillfocusImageWidth)
	ON_EN_KILLFOCUS(IDC_ALLOW_MAS_ERROR, OnKillfocusAllowMasError)
	ON_NOTIFY(NM_KILLFOCUS, IDC_COMPRESS_RELATIVE_LIST, OnKillfocusCompressRelativeList)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CHyperspectralDataCompressDlg message handlers
LRESULT CHyperspectralDataCompressDlg::OnHyperspectralDataCompressRecvMessage(WPARAM wParam, LPARAM lParam)
{
	int i;
	DWORD dwStatus;
	CString s;
	if(wParam==0)
	{
		i=lParam;
		m_NoticeNumberString.Format(_T("完成 %d%%"),i);
		UpdateData(FALSE);
	}
	else if(wParam==1)
	{
		if(CompressThreadRun==FALSE)
		{
			if (m_pCompressThread != NULL)
			{
				VERIFY(::GetExitCodeThread(m_pCompressThread->m_hThread, &dwStatus));
				if (dwStatus != STILL_ACTIVE)
				{
					m_NoticeString=_T("");
					m_NoticeNumberString=_T("");
					GetDlgItem(IDC_COMPRESS_CANCEL)->SetWindowText(_T("取消"));
					GetDlgItem(IDC_COMPRESS_OK)->EnableWindow(TRUE);
					GetDlgItem(IDC_COMPRESS_FILE_NAME)->EnableWindow(TRUE);
					GetDlgItem(IDC_MULBAND_IMAGE_FILENAME_FIND_BUTTON)->EnableWindow(TRUE);
					GetDlgItem(IDC_OMIS_I_PREDICT_BAND)->EnableWindow(TRUE);
					GetDlgItem(IDC_OMIS_II_PREDICT_BAND)->EnableWindow(TRUE);
					GetDlgItem(IDC_TEST_PREDICT_BAND)->EnableWindow(TRUE);
					GetDlgItem(IDC_AUX_DATA_WIDTH)->EnableWindow(TRUE);
					GetDlgItem(IDC_LINES_PER_BLOCK)->EnableWindow(TRUE);
					GetDlgItem(IDC_COMPRESS_RELATIVE_LIST)->EnableWindow(TRUE);
					GetDlgItem(IDC_INPUT_FILE_NAME)->EnableWindow(TRUE);
					GetDlgItem(IDC_ALLOW_MAS_ERROR)->EnableWindow(TRUE);
					GetDlgItem(IDC_COMPRESS_FILE_NAME)->EnableWindow(TRUE);
					if(PureImageFormat==TRUE)
					{
						GetDlgItem(IDC_BITS_PER_PIXEL)->EnableWindow(TRUE);
						GetDlgItem(IDC_BYTES_PER_PIXEL)->EnableWindow(TRUE);
						GetDlgItem(IDC_IMAGE_HEIGHT)->EnableWindow(TRUE);
						GetDlgItem(IDC_IMAGE_WIDTH)->EnableWindow(TRUE);
					}
					delete m_pCompressThread;
					m_pCompressThread = NULL;
					if(CompressComplete==TRUE)
					{
						CompressComplete=FALSE;
						if((m_AllowMaxErr+AllowMaxErrStep)<=AllowMaxErrEnd)
						{
							m_AllowMaxErr+=AllowMaxErrStep;
							m_ComperssFileName=GetCompressFileName(m_InputFileName);
							UpdateData(FALSE);
							PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,4,0);
						}
						else 
						{
							UpdateData(FALSE);
							if(AllowReport==TRUE)
							{
								OutString(Report);
							}
						}
					}
					else
					{
						UpdateData(FALSE);
						if(AllowReport==TRUE)
						{
							OutString(Report);
						}
					}
				}
				else PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,1,lParam);
			}
		}
		else
		{
			CompressThreadRun=FALSE;
			PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,1,lParam);
			//消息处理过程可为别的线程提供运行的机会。
		}
	}
	else if(wParam==2)
	{
		i=lParam;
		m_NoticeNumberString.Format(_T("完成 %d%%"),i);
		UpdateData(FALSE);
	}
	else if(wParam==3)
	{
		if(TestRelativeThreadRun==FALSE)
		{
			if (m_pTestRelativeThread != NULL)
			{
				VERIFY(::GetExitCodeThread(m_pTestRelativeThread->m_hThread, &dwStatus));
				if (dwStatus != STILL_ACTIVE)
				{
					m_NoticeString=_T("");
					m_NoticeNumberString=_T("");
					UpdateData(FALSE);
					GetDlgItem(IDC_COMPRESS_CANCEL)->SetWindowText(_T("取消"));
					if(TestRelativeComplate==TRUE)
					{
						UpdateControlList(FALSE);
						if(AllowReport==TRUE)m_ComperssFileName=GetCompressFileName(m_InputFileName);
					}
					delete m_pTestRelativeThread;
					m_pTestRelativeThread = NULL;
				}
				else PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,3,lParam);
			}
		}
		else
		{
			TestRelativeThreadRun=FALSE;
			PostMessage(WM_USER_HYPERSPECTRAL_COMPRESS_DLG,3,lParam);
			//消息处理过程可为别的线程提供运行的机会。
		}
	}
	else if(wParam==4)
	{
		OnCompressOk();
	}
	else if(wParam==5)
	{
		UpdateData(FALSE);
	}
	return 0;
}
void CHyperspectralDataCompressDlg::OutString(CString str,LPCTSTR lpOutFile)
{
	CString OutFile;
	int i;
	TCHAR asc[512],WindowsDir[512];
	GetWindowsDirectory(WindowsDir,512);
	LPSTR lpStr;
	CMapFile DMF;
	if(lpOutFile==NULL)
	{
		strcpy(asc,(LPCTSTR)m_ComperssFileName);
		for(i=m_ComperssFileName.GetLength()-1;i>=0&&asc[i]!='.'&&asc[i]!='\\'&&asc[i]!=':';i--);
		if(asc[i]=='.')asc[i-3]='\0';
		OutFile=asc;
		OutFile+=_T(".txt");
	}
	else OutFile=lpOutFile;
	CString windpath;
	windpath=WindowsDir;
	windpath+=_T("\\NOTEPAD ");
	windpath+=OutFile;
	if(DMF.CreateFileMap((LPCTSTR)OutFile,
				PAGE_READWRITE,
				str.GetLength()+1,
				NULL,
				CREATE_ALWAYS)!=NULL)
	{//输出产生的结果
		lpStr=(LPSTR)DMF.MapOfFile(0,str.GetLength()+1);
		MoveMemory(lpStr,str.GetBuffer(str.GetLength()),str.GetLength());
		lpStr[str.GetLength()]='\0';
		DMF.Close();
	}
	WinExec( (LPCTSTR)windpath,SW_SHOWNORMAL); 
}

BOOL CHyperspectralDataCompressDlg::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);
		}
	}

	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	Report.Empty();
	AllowReport=FALSE;
	AllowMaxErrBgn=AllowMaxErrEnd=0;
	AllowMaxErrStep=1;

	GetDlgItem(IDC_IMAGE_WIDTH)->EnableWindow(FALSE);
	GetDlgItem(IDC_BITS_PER_PIXEL)->EnableWindow(FALSE);
	GetDlgItem(IDC_BYTES_PER_PIXEL)->EnableWindow(FALSE);
	GetDlgItem(IDC_IMAGE_HEIGHT)->EnableWindow(FALSE);
	AllowMaxErrBgn=AllowMaxErrEnd=m_AllowMaxErr=24;
	m_AllowMaxErrorString=_T("24");
	m_AuxDataWidth=0;

	CRect ListRect;
	LV_COLUMN lvc;

	lvc.mask = LVCF_TEXT | LVCF_SUBITEM | LVCF_WIDTH;

	GetDlgItem(IDC_COMPRESS_RELATIVE_LIST)->GetWindowRect(&ListRect);
	int ControlWidth=ListRect.right-ListRect.left-20;

	lvc.iSubItem =0;
	lvc.pszText = _T("波段");
	lvc.cx = ControlWidth/2;
	m_RelativeList.InsertColumn(0,&lvc);

	lvc.iSubItem =1;
	lvc.pszText = _T("预测");
	lvc.cx = ControlWidth/2;
	m_RelativeList.InsertColumn(1,&lvc);
	DWORD ExStyle;
	ExStyle=m_RelativeList.GetExtendedStyle();
	m_RelativeList.SetExtendedStyle(ExStyle|
									LVS_EX_FULLROWSELECT);
	
	UpdateData(FALSE);
	return TRUE;  // return TRUE  unless you set the focus to a control
}

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

void CHyperspectralDataCompressDlg::OnDestroy()
{
	CDialog::OnDestroy();
}

// 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 CHyperspectralDataCompressDlg::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();
	}
}

HCURSOR CHyperspectralDataCompressDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CHyperspectralDataCompressDlg::OnClose() 
{
	// 响应窗口关闭
	
	CDialog::OnClose();
}

void CHyperspectralDataCompressDlg::OnCompressCancel() 
{
	//不作压缩,退出
	if(CompressThreadRun==TRUE)CompressThreadRun=FALSE;
	else if(TestRelativeThreadRun==TRUE)TestRelativeThreadRun=FALSE;
	else PostMessage(WM_CLOSE,0,0);
}

void CHyperspectralDataCompressDlg::OnKillfocusCompressFileName() 
{
	// 检测高光谱文件,以确定相关参数
	UpdateData(TRUE);
	CFile f;
	LPCTSTR Exfilename;
	Exfilename=((LPCTSTR)m_InputFileName)+m_InputFileName.GetLength()-3;
	if(FileNumber==0)
	{//文件名并不是通过浏览得到的,而是直接输入文件名。
		if(::TestTheFileExist((LPCTSTR)m_InputFileName)==TRUE)
		{
			if(strcmp(Exfilename,_T("raw"))==0||strcmp(Exfilename,_T("RAW"))==0)
			{
				m_BandNumber=1;
				GetDlgItem(IDC_IMAGE_WIDTH)->EnableWindow(TRUE);
				GetDlgItem(IDC_BITS_PER_PIXEL)->EnableWindow(TRUE);
				GetDlgItem(IDC_BYTES_PER_PIXEL)->EnableWindow(TRUE);
				GetDlgItem(IDC_IMAGE_HEIGHT)->EnableWindow(TRUE);
				PureImageFormat=TRUE;
				m_AuxDataWidth=BFILE_AUXDATA_WIDTH;
				m_LinesPerBlock=MAX_LINES_PER_BLOCK;
				InitPredictBandString();
				m_ComperssFileName=GetCompressFileName(m_InputFileName);
				UpdateData(FALSE);
			}
			else if(f.Open((LPCTSTR)m_InputFileName,CFile::modeRead)==TRUE)
			{//打开多波段文件
				stAuxMsg *lpHead=new stAuxMsg;
				f.Read(lpHead,sizeof(stAuxMsg));
				f.Close();
				m_BandNumber=lpHead->iImageBand;

⌨️ 快捷键说明

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