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

📄 substretchdlg.cpp

📁 c语言实现的遥感图像处理的一些基本操作
💻 CPP
字号:
// SubStretchDlg.cpp : implementation file
//

#include "stdafx.h"
#include "RSImageStar.h"
#include "SubStretchDlg.h"

#include "MainFrm.h"
#include "RSImageStarDoc.h"
#include "RSImageStarView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CSubStretchDlg dialog
extern HCURSOR          hCusorMove;

CSubStretchDlg::CSubStretchDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CSubStretchDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CSubStretchDlg)
	m_check = FALSE;
	//}}AFX_DATA_INIT
	flagnode=0;
	ncount=0;
	flagbutton=false;
	tmp=NULL;
}


void CSubStretchDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CSubStretchDlg)
	DDX_Control(pDX, IDC_STATIC1, m_static);
	DDX_Check(pDX, IDC_CHECK1, m_check);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CSubStretchDlg, CDialog)
	//{{AFX_MSG_MAP(CSubStretchDlg)
	ON_WM_PAINT()
	ON_BN_CLICKED(IDC_CHECK1, OnCheck1)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_DESTROY()
	ON_BN_CLICKED(ID_IMAGE_RESTORE, OnImageRestore)
	ON_BN_CLICKED(ID_IMAGE_DELETE, OnImageDelete)
	ON_WM_SIZE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CSubStretchDlg message handlers

BOOL CSubStretchDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	// TODO: Add extra initialization here
	m_check=true;
//	m_static.ShowWindow(true);
	CButton* button= (CButton*)GetDlgItem(IDC_CHECK1);
	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return 0;
    CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();
    if(lpView->m_pDoc->m_Dib->m_hPreDib==NULL)
		return false;
    lpView->m_PreView=true;
	DWORD dwSize = GlobalSize(lpView->m_pDoc->m_Dib->m_hPreDib);
    ASSERT(dwSize);
  	LPBYTE lpDib = (LPBYTE) ::GlobalLock(lpView->m_pDoc->m_Dib->m_hPreDib);
    ASSERT(lpDib);
	if( m_hDib != NULL )
    {
		GlobalUnlock( m_hDib );
        GlobalFree( m_hDib );
        m_hDib = NULL;
    }
	m_hDib      = ::GlobalAlloc(GMEM_MOVEABLE | GMEM_ZEROINIT, dwSize);
   	LPBYTE lpfile = (LPBYTE) ::GlobalLock(m_hDib);
	memcpy(lpfile,lpDib,dwSize); 
    ::GlobalUnlock(lpView->m_pDoc->m_Dib->m_hPreDib);
    ::GlobalUnlock(m_hDib);
    button->SetCheck(m_check);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CSubStretchDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	// TODO: Add your message handler code here
	// Do not call CDialog::OnPaint() for painting messages
}



void CSubStretchDlg::OnCheck1() 
{
	// TODO: Add your control notification handler code here
    m_check=m_check?false:true;
	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return;
    CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();
    if(m_check)
	{
        lpView->m_PreView=true;
 		ImageStretch();
		lpView->Invalidate();
	}
	else
	{
       lpView->m_PreView=false;
	   lpView->Invalidate();
	}
}

void CSubStretchDlg::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    CRect rect;
	m_static.GetWindowRect(&rect);
    ScreenToClient(&rect);
	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return ;
	CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();

	if(rect.PtInRect(point))
	{
		if(!flagbutton)
		{
			struct node *temp;
			temp=m_static.head;	
			int flag=0;
			while((temp!=NULL)&&(flag==0))
			{
				if((abs(point.x-temp->x-rect.left)<8)&&(abs(point.y-temp->y-rect.top)<8))
				{
					SetCursor(hCusorMove);
					flag=1;
				}
				temp=temp->next;
			}
			if(!flag)
				SetCursor(LoadCursor(NULL, IDC_CROSS)); 
		}
		else
		{
            if(flagnode)
			{
			   SetCursor(hCusorMove);
			   m_static.MoveNode(tmp,CPoint(point.x-rect.left,point.y-rect.top));
               if(m_check)
			   {
			      ImageStretch();
                  lpView->Invalidate();
			   }
			}
			else
				SetCursor(LoadCursor(NULL, IDC_CROSS)); 
		}
	}
	CDialog::OnMouseMove(nFlags, point);
}

void CSubStretchDlg::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    CRect rect;
	m_static.GetWindowRect(&rect);
    ScreenToClient(&rect);
	flagbutton=true;

	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return ;
	CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();

	if(rect.PtInRect(point))
	{
		struct node *temp;
		temp=m_static.head;
		int flag=0;
		while((temp!=NULL)&&(flag==0))
		{
		   if((abs(point.x-temp->x-rect.left)<8)&&(abs(point.y-temp->y-rect.top)<8))
		   {
			   SetCursor(hCusorMove);
			   flag=1;
			   flagnode=1;
			   tmp=temp;
		   }  
		   temp=temp->next;
		}
		if(!flag)
		{
		    flagnode=0;
			SetCursor(LoadCursor(NULL, IDC_CROSS)); 
            m_static.AddNode(CPoint(point.x-rect.left,point.y-rect.top));
			if(m_check)
			{
				ImageStretch();
				lpView->Invalidate();
			}
		}
	}
	CDialog::OnLButtonDown(nFlags, point);
}

void CSubStretchDlg::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
    CRect rect;
	m_static.GetWindowRect(&rect);
    ScreenToClient(&rect);
	flagbutton=false;
	if(rect.PtInRect(point))
	{
		if(flagnode==1)
		   SetCursor(hCusorMove);
		else
		   SetCursor(LoadCursor(NULL, IDC_CROSS)); 
	}
	CDialog::OnLButtonUp(nFlags, point);
}

void CSubStretchDlg::ImageStretch()
{
	if (m_hDib == NULL)
		return ;
	
	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return ;
	CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();
	
	LPBYTE lpDib = (LPBYTE) ::GlobalLock(lpView->m_pDoc->m_Dib->m_hPreDib);
	ASSERT(lpDib);
	LPBITMAPINFO lpbmi=(LPBITMAPINFO)lpDib;
	int numcolor=lpView->m_pDoc->m_Dib->GetColorMapNum(lpbmi->bmiHeader.biBitCount);
	int DibInfoSize = sizeof(BITMAPINFOHEADER) + numcolor*sizeof(RGBQUAD);
	LPBYTE lpdata=lpDib+DibInfoSize;
	
	int width=lpbmi->bmiHeader.biWidth;
	int width1=(lpbmi->bmiHeader.biWidth*lpbmi->bmiHeader.biBitCount/8+3)&~3;
	int height=lpbmi->bmiHeader.biHeight;
	
	LPBYTE lpfile = (LPBYTE) ::GlobalLock(m_hDib);
	LPBITMAPINFO lpbmi1=(LPBITMAPINFO)lpfile;
	
	LPBYTE lpdata1=lpfile+DibInfoSize;   
    
    struct node *tmp1=m_static.head;
    CPoint segpoint[128];
	float scale[128];
	int m=0;
    while(tmp1!=NULL)
	{
		segpoint[m].x=tmp1->x;
		segpoint[m].y=(255-tmp1->y);
		if(tmp1->next!=NULL)
		   scale[m]=-(tmp1->x-tmp1->next->x)/float(tmp1->y-tmp1->next->y);
		tmp1=tmp1->next;
		m++;
	}
	for(int i=0;i<height;i++)
	{
	       RGBTRIPLE* pixptr = (RGBTRIPLE*)(lpdata1);
		   RGBTRIPLE* pixptr1 = (RGBTRIPLE*)(lpdata);
		   for (int x=0;x<width; x++)
		   {
			   int flag=1;
			   m=0;
			   while(flag)
			   {
				   if(pixptr[x].rgbtRed<segpoint[m].x)
				   {
					   pixptr1[x].rgbtRed = min(segpoint[m-1].y+
						   int((pixptr[x].rgbtRed-segpoint[m-1].x)*scale[m-1]),255);
					   flag=0;
				   }
				   m++;
			   }
			   flag=1;
			   m=0;
			   while(flag)
			   {
				   if(pixptr[x].rgbtGreen<segpoint[m].x)
				   {
					   pixptr1[x].rgbtGreen = min(segpoint[m-1].y+
						   int((pixptr[x].rgbtGreen-segpoint[m-1].x)*scale[m-1]),255);
					   flag=0;
				   }
				   m++;
			   }
			   flag=1;
			   m=0;
			   while(flag)
			   {
				   if(pixptr[x].rgbtBlue<segpoint[m].x)
				   {
					   pixptr1[x].rgbtBlue = min(segpoint[m-1].y+
						   int((pixptr[x].rgbtBlue-segpoint[m-1].x)*scale[m-1]),255);
					   flag=0;
				   }
				   m++;
			   }
		   }
		   lpdata+=width1;
		   lpdata1+=width1;			
	}
  ::GlobalUnlock(lpView->m_pDoc->m_Dib->m_hPreDib);
  ::GlobalUnlock(m_hDib);
}

void CSubStretchDlg::OnDestroy() 
{
	CDialog::OnDestroy();
	
	// TODO: Add your message handler code here
    if(m_hDib!=NULL)
	{
		GlobalUnlock(m_hDib);
		GlobalFree(m_hDib);
		m_hDib=NULL;
	}	
	ncount=0;
	struct node *temp;
    tmp=m_static.head;
    while(tmp!=NULL)
	{
		pos[ncount].x=tmp->x;
		pos[ncount].y=(255-tmp->y);
		if(tmp->next!=NULL)
			scale[ncount]=-(tmp->x-tmp->next->x)/float(tmp->y-tmp->next->y);
		temp=tmp;
		tmp=tmp->next;
		free(temp);
		ncount++;
	}
	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return ;
    CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();
	lpView->m_PreView=false; 
}

void CSubStretchDlg::OnImageRestore() 
{
	// TODO: Add your control notification handler code here
    m_static.RestoreNode();

	CFrameWnd* lpFrameWnd = STATIC_DOWNCAST(CFrameWnd, AfxGetMainWnd());
	if( lpFrameWnd == NULL ) return;
    CRSImageStarView* lpView = (CRSImageStarView*)lpFrameWnd->GetActiveFrame()->GetActiveView();
    if(lpView->m_pDoc->m_Dib->m_hPreDib==NULL)
		return;
	DWORD dwSize = GlobalSize(lpView->m_pDoc->m_Dib->m_hPreDib);
    ASSERT(dwSize);
  	LPBYTE lpDib = (LPBYTE) ::GlobalLock(lpView->m_pDoc->m_Dib->m_hPreDib);
    ASSERT(lpDib);
   	LPBYTE lpfile = (LPBYTE) ::GlobalLock(m_hDib);

	memcpy(lpDib,lpfile,dwSize); 
    ::GlobalUnlock(lpView->m_pDoc->m_Dib->m_hPreDib);
    ::GlobalUnlock(m_hDib);
	lpView->Invalidate();
}

void CSubStretchDlg::OnImageDelete() 
{
	// TODO: Add your control notification handler code here
    m_static.DeleteNode();
	if(m_check)
	   ImageStretch();
}

⌨️ 快捷键说明

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