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

📄 mainfrm.cpp

📁 Led识别与统计系统,基于DELPHI 7.0下
💻 CPP
📖 第 1 页 / 共 4 页
字号:
// MainFrm.cpp : implementation of the CMainFrame class
//

#include "stdafx.h"
#include "HVViewer.h"

#include "MainFrm.h"
#include "..\..\inc\hvutil.h"

#include "Adjustdlg.h"
#include "WBDlg.h"
#include "Shlwapi.h"
#include "math.h"
#include "DlgGamma.h"

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



//#define  GAMMA_CORRECT 1
/////////////////////////////////////////////////////////////////////////////
// CMainFrame

IMPLEMENT_DYNCREATE(CMainFrame, CFrameWnd)

BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
	//{{AFX_MSG_MAP(CMainFrame)
	ON_WM_PAINT()
	ON_WM_WINDOWPOSCHANGING()
	ON_WM_CLOSE()
	ON_WM_SIZE()
	ON_WM_CREATE()
	ON_COMMAND(ID_VIEW_ADJUST, OnViewAdjust)
	ON_COMMAND(ID_VIEW_SNAP, OnViewSnap)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SNAP, OnUpdateViewSnap)
	ON_COMMAND(ID_FILE_OPEN_ONE, OnFileOpenOne)
	ON_COMMAND(ID_FILE_SAVE_AS_ONE, OnFileSaveAsOne)
	ON_COMMAND(ID_VIEW_WB, OnViewWb)
	ON_UPDATE_COMMAND_UI(ID_FILE_PRINT_PREVIEW, OnUpdateFilePrintPreview)
	ON_COMMAND(ID_VIEW_CONVERSION_BG, OnViewConversionBg)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CONVERSION_BG, OnUpdateViewConversionBg)
	ON_COMMAND(ID_VIEW_CONVERSION_GB, OnViewConversionGb)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CONVERSION_GB, OnUpdateViewConversionGb)
	ON_COMMAND(ID_VIEW_CONVERSION_GR, OnViewConversionGr)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CONVERSION_GR, OnUpdateViewConversionGr)
	ON_COMMAND(ID_VIEW_CONVERSION_RG, OnViewConversionRg)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CONVERSION_RG, OnUpdateViewConversionRg)
	ON_COMMAND(ID_VIEW_COLOR, OnViewColor)
	ON_UPDATE_COMMAND_UI(ID_VIEW_COLOR, OnUpdateViewColor)
	ON_COMMAND(ID_VIEW_BW, OnViewBw)
	ON_UPDATE_COMMAND_UI(ID_VIEW_BW, OnUpdateViewBw)
	ON_COMMAND(ID_HELP_WEBSITE, OnHelpWebsite)
	ON_COMMAND(ID_HELP_MAILTO, OnHelpMailto)
	ON_COMMAND(ID_VIEW_CONTINUATION, OnViewContinuation)
	ON_UPDATE_COMMAND_UI(ID_VIEW_CONTINUATION, OnUpdateViewContinuation)
	ON_COMMAND(ID_VIEW_TRIGGER, OnViewTrigger)
	ON_UPDATE_COMMAND_UI(ID_VIEW_TRIGGER, OnUpdateViewTrigger)
	ON_COMMAND(ID_VIEW_SNAPSHOT, OnViewSnapshot)
	ON_UPDATE_COMMAND_UI(ID_VIEW_SNAPSHOT, OnUpdateViewSnapshot)
	ON_UPDATE_COMMAND_UI(ID_VIEW_WB, OnUpdateViewWb)
	ON_COMMAND(ID_CONVERSION_BEST, OnConversionBest)
	ON_UPDATE_COMMAND_UI(ID_CONVERSION_BEST, OnUpdateConversionBest)
	ON_COMMAND(ID_CONVERSION_FAST, OnConversionFast)
	ON_UPDATE_COMMAND_UI(ID_CONVERSION_FAST, OnUpdateConversionFast)
	ON_COMMAND(ID_CONVERSION_NORMAL, OnConversionNormal)
	ON_UPDATE_COMMAND_UI(ID_CONVERSION_NORMAL, OnUpdateConversionNormal)
	ON_COMMAND(ID_VIEW_MODE0, OnViewMode0)
	ON_UPDATE_COMMAND_UI(ID_VIEW_MODE0, OnUpdateViewMode0)
	ON_COMMAND(ID_VIEW_MODE1, OnViewMode1)
	ON_UPDATE_COMMAND_UI(ID_VIEW_MODE1, OnUpdateViewMode1)
	ON_COMMAND(ID_VIEW_MODE2, OnViewMode2)
	ON_UPDATE_COMMAND_UI(ID_VIEW_MODE2, OnUpdateViewMode2)
	ON_COMMAND(ID_GAMMA_CORRECT, OnGammaCorrect)
	ON_UPDATE_COMMAND_UI(ID_GAMMA_CORRECT, OnUpdateGammaCorrect)
	ON_COMMAND(ID_CANCEL_GAMMA, OnCancelGamma)
	ON_UPDATE_COMMAND_UI(ID_CANCEL_GAMMA, OnUpdateCancelGamma)
	ON_COMMAND(ID_GET_BAD_PIEXES, OnGetBadPiexes)
	ON_UPDATE_COMMAND_UI(ID_GET_BAD_PIEXES, OnUpdateGetBadPiexes)
	ON_COMMAND(ID_ELIMINATE_BAD_PIXELS, OnEliminateBadPixels)
	ON_UPDATE_COMMAND_UI(ID_ELIMINATE_BAD_PIXELS, OnUpdateEliminateBadPixels)
	ON_COMMAND(ID_GET_TEMPLATE, OnGetTemplate)
	ON_UPDATE_COMMAND_UI(ID_GET_TEMPLATE, OnUpdateGetTemplate)
	ON_COMMAND(ID_TEMPLATE_CORRECT, OnTemplateCorrect)
	ON_UPDATE_COMMAND_UI(ID_TEMPLATE_CORRECT, OnUpdateTemplateCorrect)
	ON_COMMAND(ID_SNAPSPEED_NORMAL, OnSnapspeedNormal)
	ON_UPDATE_COMMAND_UI(ID_SNAPSPEED_NORMAL, OnUpdateSnapspeedNormal)
	ON_COMMAND(ID_SNPASPEED_HIGH, OnSnpaspeedHigh)
	ON_UPDATE_COMMAND_UI(ID_SNPASPEED_HIGH, OnUpdateSnpaspeedHigh)
	//}}AFX_MSG_MAP

	ON_MESSAGE(WM_CHANGE_SNAP, OnSnapMessage)
	ON_MESSAGE(WM_GET_RATIO, OnGetRatioMessage)

END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMainFrame construction/destruction

CMainFrame::CMainFrame()
{
	// TODO: add member initialization code here
	m_hhv			= NULL;

	m_SnapMode		= CONTINUATION;	
	m_Resolution	= RES_MODE0;
	m_lADCLevel		= ADC_LEVEL2;
	
	
	for (int i = 0; i < 4; i++)
	{
		m_lGain[i]	= 8;
	}
	
	m_rcOutputWindow.SetRect(0, 0, 0, 0);
	
	m_ImageMode		= HV_COLOR;
	m_Layout	= BAYER_GB;
	
	m_dRatioR		= 1.0;
	m_dRatioG		= 1.0;
	m_dRatioB		= 1.0;
	
	m_nMaxWidth		= 0;
	m_nMaxHeight	= 0;

	m_nScreenWidth	= 0;
	m_nScreenHeight = 0;
	m_nScreenBits	= 0;

	m_pBmpInfo		= (BITMAPINFO *)m_chBmpBuf;

	m_hPalette		= NULL;
	m_hOldPal		= NULL;

	m_dwCount		= 0;
	m_dwStart		= 0;

	m_nMarginLeft	= 0;
	m_nMarginTop	= 0;
	m_nMarginRight	= 0;
	m_nMarginBottom	= 0;

	
	m_bMinimized	= FALSE;
	m_bSnapping		= FALSE;



	//颜色查值表
	for(i=0;i<256;i++)
	{
		m_pLutGamma[i] = i;		

		m_pLutR[i] = i;
		m_pLutG[i] = i;
		m_pLutB[i] = i;		

	}



	m_bIsGammaCorrect = FALSE;
	m_dGammRatio = 1.0;

	//映射文件句柄
	m_hFileMapping = NULL;
	m_pLutMapping = NULL;;

	m_lTintUpper =60;
	m_lTintLower = 1000;
	m_ConversionType = BAYER2RGB_NEIGHBOUR;

	m_bIsToGetBadPixel = FALSE;
	m_bIsToRemoveBadPixel = FALSE;
	m_bIsToGetTemplate = FALSE;
	m_bIsTemplateCorrect = FALSE;
	m_nTempTimes =0;

	m_SnapSpeed = HIGH_SPEED;
	m_nHBlanking = 0;
	m_nVBlanking = 0;

	m_bIsSnapSpeedSprted = FALSE;   //本摄像机是否支持设置采集速度
	m_bIsBlkSizeSprted = FALSE;   //本摄像机是否支持设置取得消隐取值范围	
}



CMainFrame::~CMainFrame()
{
	if (m_pImageBuffer) {
		delete []m_pImageBuffer;
	}
	if (m_pRawBuffer) {
		delete []m_pRawBuffer;
	}
	if (m_hPalette) {
		DeleteObject(m_hPalette);
	}

	if(m_pLutMapping)
	{
		// 从进程的地址空间撤消文件数据映像
		UnmapViewOfFile(m_pLutMapping);		
	}
	if(m_hFileMapping)
	{
		// 关闭文件映射对象
		CloseHandle(m_hFileMapping);
	}

	if(m_pPatternBuf)
	{
		delete[] m_pPatternBuf;
	}
	if(m_pBadPixelBuf)
	{
		delete[] m_pBadPixelBuf;
	}

}

BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs)
{
	if( !CFrameWnd::PreCreateWindow(cs) )
		return FALSE;
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs
	
	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// CMainFrame diagnostics

#ifdef _DEBUG
void CMainFrame::AssertValid() const
{
	CFrameWnd::AssertValid();
}

void CMainFrame::Dump(CDumpContext& dc) const
{
	CFrameWnd::Dump(dc);
}

#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMainFrame message handlers

void CMainFrame::OnUpdateFilePrintPreview(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here

}


void CMainFrame::OnPaint()
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here	
	CString strTitle;
	strTitle.Format("%s Viewer [%d x %d]", m_strDeviceType, m_rcOutputWindow.Width(), m_rcOutputWindow.Height());
	SetWindowText(strTitle);	
	// Do not call CFrameWnd::OnPaint() for painting messages
}



void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
	CFrameWnd::OnSize(nType, cx, cy);	

	// TODO: Add your message handler code here
	CView *pView = GetActiveView();
	if (pView){
		CRect rcClient;
		pView->GetClientRect(rcClient);
		m_rcOutputWindow = rcClient;
		//The video output window is invalid , and then adjust it
		m_rcOutputWindow.right	-= rcClient.Width() %4;
		m_rcOutputWindow.bottom -= rcClient.Height() % 2;

		//在设置输出窗口前,停止采集
		if (m_bSnapping)
		{
			HVStopSnap(m_hhv);
		}
		HVSTATUS status = HVSetOutputWindow(m_hhv, 
									m_rcOutputWindow.left,
									m_rcOutputWindow.top,
									m_rcOutputWindow.Width(),
									m_rcOutputWindow.Height()			
									);  
		//在设置输出窗口后,开始采集
		if (m_bSnapping)
		{
			//只定义一个缓冲区,
			BYTE * ppBuffer[1];
			ppBuffer[0] = m_pRawBuffer;
			status = HVStartSnap(m_hhv,ppBuffer,1);	
		}

		HV_VERIFY(status);
		TRACE("%d : %d\n", m_rcOutputWindow.Width(), m_rcOutputWindow.Height());
		
		//Reset frame count
		m_dwCount = 0;
		m_dwStart = GetTickCount();
		
		//When outputwindow changes, change the exposure 
		SetExposureTime(GetOutputWinWdith(),m_lTintUpper,m_lTintLower);

	}  
}




void CMainFrame::OnWindowPosChanging(WINDOWPOS FAR* lpwndpos) 
{
	int nMaxWidth = 0, nMaxHeight = 0;
	
	CFrameWnd::OnWindowPosChanging(lpwndpos);
	
	// TODO: Add your message handler code here
	TRACE("Call OnWindowPosChanging \n");
	
	if (m_Resolution == RES_MODE0){
		nMaxWidth	= m_nScreenWidth - m_nMarginLeft - m_nMarginRight;
		nMaxHeight	= m_nScreenHeight - m_nMarginTop - m_nMarginBottom;
	}
	else{
		nMaxWidth	= m_nMaxWidth;
		nMaxHeight	= m_nMaxHeight;
	}
	
	if ( IsIconic()) {
		m_bMinimized = TRUE;	
	}
	else{
		if (IsZoomed()) {
			if (m_Resolution == RES_MODE0){
				lpwndpos->x		= 0;
				lpwndpos->y		= 0;
			}
			lpwndpos->cx	= nMaxWidth + m_nMarginLeft + m_nMarginRight;
			lpwndpos->cy	= nMaxHeight + m_nMarginTop + m_nMarginBottom;
		}
		
		if (m_bMinimized ) {
			m_bMinimized = FALSE;
		}
		else {
			if ((lpwndpos->cx - m_nMarginLeft - m_nMarginRight) > m_nMaxWidth) {
				lpwndpos->cx = m_nMaxWidth + m_nMarginLeft + m_nMarginRight;
			}
			lpwndpos->cx -= (lpwndpos->cx - m_nMarginLeft - m_nMarginRight) % 4;	
			TRACE("cx : %d, cy : %d \n", lpwndpos->cx, lpwndpos->cy);
			
			if ((lpwndpos->cy - m_nMarginTop - m_nMarginBottom) > m_nMaxHeight) {
				lpwndpos->cy = m_nMaxHeight + m_nMarginTop + m_nMarginBottom;
			}
			lpwndpos->cy -= (lpwndpos->cy - m_nMarginTop - m_nMarginBottom) % 2;
		}
	}

}



void CMainFrame::UpdateMargin()
{
	CRect rcView, rcFrame;

	CView *pView = GetActiveView();
	if(pView){
		pView->GetClientRect(&rcView);
		pView->ClientToScreen(&rcView);
		
		GetWindowRect(&rcFrame);
		
		m_nMarginLeft	= rcView.left - rcFrame.left;
		m_nMarginTop	= rcView.top - rcFrame.top;
		m_nMarginRight	= rcFrame.right - rcView.right;
		m_nMarginBottom = rcFrame.bottom - rcView.bottom;		
	}
}




void CMainFrame::AdjustWindow()
{
	CRect rcFrame;
	GetWindowRect(&rcFrame);
	MoveWindow(&rcFrame);
}





void CMainFrame::InitParamFromINI()
{
	int n = 0;

	m_Resolution	= (HV_RESOLUTION)GetPrivateProfileInt("Video Parameter", "Resolution", RES_MODE0, "HVViewer.ini" );

	m_SnapMode	= (HV_SNAP_MODE)GetPrivateProfileInt("Video Parameter", "SnapMode", CONTINUATION, "HVViewer.ini" );
	
	m_ImageMode		= (HV_IMAGE_MODE)GetPrivateProfileInt("Video Parameter", "ImageMode", HV_COLOR, "HVViewer.ini" );
	
	m_Layout	= (HV_BAYER_LAYOUT)GetPrivateProfileInt("Video Parameter", "BayerLayout", BAYER_GR, "HVViewer.ini" );

	m_lTintUpper	= (long)GetPrivateProfileInt("Video Parameter", "TintUpper", 60, "HVViewer.ini" );
	m_lTintLower	= (long)GetPrivateProfileInt("Video Parameter", "TintLower", 1000, "HVViewer.ini" );

	m_lADCLevel	= (long)GetPrivateProfileInt("Video Parameter", "ADCLevel", ADC_LEVEL1, "HVViewer.ini" );
	
	m_lGain[0]	= (long)GetPrivateProfileInt("Video Parameter", "GainRCh", 8, "HVViewer.ini" );
	m_lGain[1]	= (long)GetPrivateProfileInt("Video Parameter", "GainRChG", 8, "HVViewer.ini" );
	m_lGain[2]	= (long)GetPrivateProfileInt("Video Parameter", "GainBChG", 8, "HVViewer.ini" );
	m_lGain[3]	= (long)GetPrivateProfileInt("Video Parameter", "GainBCh", 8, "HVViewer.ini" );
	
	n = GetPrivateProfileInt("Video Parameter", "RatioRL", 0, "HVViewer.ini" );
	CopyMemory(&m_dRatioR, &n, sizeof(int));
	n = GetPrivateProfileInt("Video Parameter", "RatioRH", 0, "HVViewer.ini" );
	CopyMemory((char *)&m_dRatioR + sizeof(int), &n, sizeof(int));

	n = GetPrivateProfileInt("Video Parameter", "RatioGL", 0, "HVViewer.ini" );
	CopyMemory(&m_dRatioG, &n, sizeof(int));
	n = GetPrivateProfileInt("Video Parameter", "RatioGH", 0, "HVViewer.ini" );
	CopyMemory((char *)&m_dRatioG + sizeof(int), &n, sizeof(int));

	n = GetPrivateProfileInt("Video Parameter", "RatioBL", 0, "HVViewer.ini" );
	CopyMemory(&m_dRatioB, &n, sizeof(int));
	n = GetPrivateProfileInt("Video Parameter", "RatioBH", 0, "HVViewer.ini" );
	CopyMemory((char *)&m_dRatioB + sizeof(int), &n, sizeof(int));

	m_nHBlanking	= (long)GetPrivateProfileInt("Video Parameter", "HBlanking", 0, "HVViewer.ini" );
	m_nVBlanking	= (long)GetPrivateProfileInt("Video Parameter", "VBlanking", 0, "HVViewer.ini" );
	m_SnapSpeed =  (HV_SNAP_SPEED )GetPrivateProfileInt("Video Parameter", "SnapSpeed", HIGH_SPEED, "HVViewer.ini" );
		
}




void CMainFrame::SaveParamToINI()
{
	CString strValue;

	strValue.Format("%d", m_Resolution);
	WritePrivateProfileString("Video Parameter", "Resolution", strValue, "HVViewer.ini");

	strValue.Format("%d", m_SnapMode);
	WritePrivateProfileString("Video Parameter", "SnapMode", strValue, "HVViewer.ini");

	strValue.Format("%d", m_ImageMode);
	WritePrivateProfileString("Video Parameter", "ImageMode", strValue, "HVViewer.ini");
	strValue.Format("%d", m_Layout);
	WritePrivateProfileString("Video Parameter", "BayerLayout", strValue, "HVViewer.ini");
	
	strValue.Format("%d", m_lTintLower);
	WritePrivateProfileString("Video Parameter", "TintLower", strValue, "HVViewer.ini");

	strValue.Format("%d", m_lTintUpper);
	WritePrivateProfileString("Video Parameter", "TintUpper", strValue, "HVViewer.ini");

	strValue.Format("%d", m_lADCLevel);
	WritePrivateProfileString("Video Parameter", "ADCLevel", strValue, "HVViewer.ini");

	strValue.Format("%d", m_lGain[0]);
	WritePrivateProfileString("Video Parameter", "GainRCh", strValue, "HVViewer.ini");
	strValue.Format("%d", m_lGain[1]);
	WritePrivateProfileString("Video Parameter", "GainRChG", strValue, "HVViewer.ini");
	strValue.Format("%d", m_lGain[2]);
	WritePrivateProfileString("Video Parameter", "GainBChG", strValue, "HVViewer.ini");
	strValue.Format("%d", m_lGain[3]);
	WritePrivateProfileString("Video Parameter", "GainBCh", strValue, "HVViewer.ini");

	strValue.Format("%d", *((int *)&m_dRatioR));
	WritePrivateProfileString("Video Parameter", "RatioRL", strValue, "HVViewer.ini");
	strValue.Format("%d", *((int *)&m_dRatioR + 1));
	WritePrivateProfileString("Video Parameter", "RatioRH", strValue, "HVViewer.ini");

	strValue.Format("%d", *((int *)&m_dRatioG));
	WritePrivateProfileString("Video Parameter", "RatioGL", strValue, "HVViewer.ini");
	strValue.Format("%d", *((int *)&m_dRatioG + 1));
	WritePrivateProfileString("Video Parameter", "RatioGH", strValue, "HVViewer.ini");

	strValue.Format("%d", *((int *)&m_dRatioB));
	WritePrivateProfileString("Video Parameter", "RatioBL", strValue, "HVViewer.ini");
	strValue.Format("%d", *((int *)&m_dRatioB + 1));
	WritePrivateProfileString("Video Parameter", "RatioBH", strValue, "HVViewer.ini");

	strValue.Format("%d", m_nHBlanking);
	WritePrivateProfileString("Video Parameter", "HBlanking", strValue, "HVViewer.ini");
	strValue.Format("%d", m_nVBlanking);
	WritePrivateProfileString("Video Parameter", "VBlanking", strValue, "HVViewer.ini");

	strValue.Format("%d", m_SnapSpeed);
	WritePrivateProfileString("Video Parameter", "SnapSpeed", strValue, "HVViewer.ini");	
	
}




void CMainFrame::InitSystemInfo()

⌨️ 快捷键说明

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