mainfrm.cpp

来自「一个非常全的vc编程的原程序代码是关于图像处理的!」· C++ 代码 · 共 2,610 行 · 第 1/5 页

CPP
2,610
字号

	for(i=0;i<256;i++)
		m_ThresholdDlg.m_L[i] = 0;
		
	max = max0 = max1 = max2 = 0;
	if(bitUsed == 8)
	{
		for(j=0;j<height;j++)
		{
			for( i=0;i<width;i++)
			{
				curByte = *(LPBYTE)(lpBits+j*bytesPerLine+i);
				m_ThresholdDlg.m_L[curByte] ++;
				if(m_ThresholdDlg.m_L[curByte] > max)
					max = m_ThresholdDlg.m_L[curByte];
			}
		}
	}
	else if(bitUsed == 24)
	{
		for(j=0;j<height;j++)
		{
			for(i=0;i<width;i++)
			{
				
				curB = *(BYTE*)(lpBits+j*bytesPerLine+i*3);
				m_ThresholdDlg.m_B[curB] ++;
			
				curG = *(BYTE*)(lpBits+j*bytesPerLine+i*3+1);
				m_ThresholdDlg.m_G[curG] ++;
			
				curR = *(BYTE*)(lpBits+j*bytesPerLine+i*3+2);
				m_ThresholdDlg.m_R[curR] ++;
			
				curByte = (BYTE)(curR*0.3+curG*0.59+curB*0.11);
				m_ThresholdDlg.m_L[curByte] ++;
				if(m_ThresholdDlg.m_L[curByte] > max)
					max = m_ThresholdDlg.m_L[curByte];

			}
		}
	}
	for( i=0;i<256;i++)
		m_ThresholdDlg.m_L[i] = (long)(m_ThresholdDlg.m_L[i]*100/max);
			
	m_ThresholdDlg.m_max = max;
	m_ThresholdDlg.DoModal();

	return;
	}else{
		MessageBox("请先打开一个BMP文件!", "Error");
		return;
	}
	
}

void CMainFrame::OnBinary()
{
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
	if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

    CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
	if(pDoc == NULL){
		MessageBox("请先打开一个BMP文件!", "Error");
		return;
	}
    
	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp  = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);
	
	int width		 = pDoc->m_pDib->GetWidth();
	int height		 = pDoc->m_pDib->GetHeight();
	int bytesPerLine = pDoc->m_pDib->GetWidthBytes();
	int bitUsed		 = pDoc->m_pDib->GetBitCount();
	LPBYTE curL, curB, curR, curG;
    int	   iL;
	LPBYTE lpBits = pDoc->m_pDib->GetBitsPtr();
   	int i, j;

	if(bitUsed == 8){
		for(j=0;j<height;j++){
			for(i=0;i<width;i++){
				curL = (LPBYTE)(lpBits+j*bytesPerLine+i);
				if(*curL > m_value)
					*curL = 255;
				else 
					*curL = 0;
			}
		}
	}
	else if(bitUsed == 24)
	{
		for(j=0;j<height;j++)
		{
			for(i=0;i<width;i++)
			{
				curB = (LPBYTE)(lpBits+j*bytesPerLine+i*3);
				curG = (LPBYTE)(lpBits+j*bytesPerLine+i*3+1);
				curR = (LPBYTE)(lpBits+j*bytesPerLine+i*3+2);
             	int r = *curR;
				int g = *curG;
				int b = *curB;
				iL = (int)(r*0.3+g*0.59+b*0.11);
			    if(iL > m_value)
					*curR = *curB = *curG = 255;
				else 
					*curR = *curB = *curG = 0;
			}
		}
	}
    pDoc->m_pDib->BuildBitmap();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}



void CMainFrame::OnMenuAttribute() 
{
	// TODO: Add your command handler code 
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}

	long width	   = pDoc->m_pDib->GetWidth();
	long height	   = pDoc->m_pDib->GetHeight();
	long Xpixels   = pDoc->m_pDib->GetXPelsPerMeter();
	long Ypixels   = pDoc->m_pDib->GetYPelsPerMeter();
    long bitsCount = pDoc->m_pDib->GetBitCount();
	long sizeImage = height*(pDoc->m_pDib->GetWidthBytes());
	
	m_AttributeDlg.m_Width.Format("%ld", width);
	m_AttributeDlg.m_Height.Format("%ld", height);
	m_AttributeDlg.m_XPixelsPerMeter.Format("%ld", Xpixels);
    m_AttributeDlg.m_YPixelsPerMeter.Format("%ld", Ypixels);
	m_AttributeDlg.m_SizeImage.Format("%ld", sizeImage);
	m_AttributeDlg.m_BitCount.Format("%ld", bitsCount);

	m_AttributeDlg.DoModal();
    UpdateData(FALSE);
}

void CMainFrame::OnMenuRotate180() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	//save old DIB
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp  = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->Rotate180();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuRotate90() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    

	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}
	//save old DIB
	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->Rotate90();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuRotate270() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	//save old DIB
	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->Rotate270();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuFlipHor() 
{
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}
	//save old DIB
	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->FlipHorz();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuFlipVert() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}
	//save old DIB
	pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->FlipVert();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuSelfDiff() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	int BitCount = pDoc->m_pDib->GetBitCount();
	if((BitCount == 1) || (BitCount == 4)){
		MessageBox("此操作只能用于处理灰度图或真彩色图!", "Attention");
		return;
	}
	
	//save old DIB
    pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->SelfDiff();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuRevertColor() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	int BitCount = pDoc->m_pDib->GetBitCount();
	if((BitCount == 1)||(BitCount == 4)){
		MessageBox("此操作只能用于处理灰度图或真彩色图!", "Attention");
		return;
	}
	
	//save old DIB
    pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->RevertColor();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuColor2Gray() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	int BitCount = pDoc->m_pDib->GetBitCount();
	if(BitCount != 24){
		MessageBox("此操作只能用于处理真彩色图!", "Attention");
		return;
	}
	
	//save old DIB
    pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

	pDoc->m_pDib->ColorToGray();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

void CMainFrame::OnMenuRotateCertainAngle() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){
		MessageBox("请先打开一个Bmp文件!", "Error");
		return;
	}    
	
	if(pDoc->m_operFlag == TRUE){
        m_AttentionDlg.DoModal();	
		if(pDoc->m_operFlag == TRUE)
			return;
	}

	int BitCount = pDoc->m_pDib->GetBitCount();
	if((BitCount == 1) || (BitCount == 4)){
		MessageBox("此操作只能用于处理灰度图或真彩色图!", "Attention");
		return;
	}
	
	//save old DIB
    pDoc->m_operFlag = TRUE;
	pDoc->m_pDibTmp = (CDib*)(new CDib());
	pDoc->m_pDibTmp->Create(pDoc->m_pDib->m_hBitmap);

    m_RotateAngleDlg.m_angle = 0;
	m_RotateAngleDlg.DoModal();

	CDC* pDC = CWnd->GetDC();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();
    CWnd->Invalidate();

	return;
}

BOOL CMainFrame::OnRotate()
{
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    CViewDIBView* pView = (CViewDIBView*)CWnd->GetActiveView();

    pDoc->m_operFlag = TRUE;

	pDoc->m_pDib->RotateCertainAngle(m_RotateAngleDlg.m_angle);

	CSize sizeTotal(pDoc->m_pDib->GetWidth(), pDoc->m_pDib->GetHeight());
	pView->SetScrollSizes(MM_TEXT, sizeTotal);

	CDC* pDC = CWnd->GetDC();
    CWnd->Invalidate();
    return TRUE;
}

void CMainFrame::OnMenuAutoBinary() 
{
	// TODO: Add your command handler code here
	CMDIChildWnd* CWnd = MDIGetActive(NULL);
    if(CWnd == NULL){
		MessageBox("请先打开一个窗口!", "Error");
		return;
	}

	CViewDIBDoc* pDoc = (CViewDIBDoc*)CWnd->GetActiveDocument();
    if(pDoc == NULL){

⌨️ 快捷键说明

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