📄 viewcdibview.cpp
字号:
// ViewCDibView.cpp : implementation of the CViewCDibView class
//
#include "stdafx.h"
#include "ViewCDib.h"
#include "Dib.h"
#include "dibapi.h"
#include "ViewCDibDoc.h"
#include "ViewCDibView.h"
#include <math.h>
#include "MainFrm.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView
IMPLEMENT_DYNCREATE(CViewCDibView, CScrollView)
BEGIN_MESSAGE_MAP(CViewCDibView, CScrollView)
//{{AFX_MSG_MAP(CViewCDibView)
ON_COMMAND(IDM_CHANGETOGRAY, OnChangetogray)
ON_COMMAND(IDR_SOBEL, OnSobel)
ON_COMMAND(IDR_PIXELOPER, OnPixeloper)
ON_COMMAND(IDR_FUDIAO, OnFudiao)
ON_COMMAND(IDR_MYMIND, OnMymind)
ON_COMMAND(IDR_DOSOBEL, OnDosobel)
ON_COMMAND(IDR_EDGEDETECT, OnEdgedetect)
ON_COMMAND(ID_MEDIAFILTER, OnMediafilter)
ON_COMMAND(ID_OPEN, OnOpen)
ON_COMMAND(ID_CLOSE, OnClose)
ON_COMMAND(ID_BW, OnBw)
ON_COMMAND(ID_REVER, OnRever)
ON_COMMAND(ID_CONTOUR2, OnContour2)
ON_COMMAND(ID_LAPEDGE, OnLapedge8)
ON_COMMAND(ID_XIHUA, OnXihua)
ON_COMMAND(ID_SUBPIXEL49, OnSubpixel49)
ON_COMMAND(ID_SUBPIXEL49II, OnSubpixel49ii)
//}}AFX_MSG_MAP
// Standard printing commands
ON_COMMAND(ID_FILE_PRINT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, CScrollView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, CScrollView::OnFilePrintPreview)
ON_MESSAGE(WM_REALIZEPAL, OnRealizePal)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView construction/destruction
#define pi 3.1415926;
int T=128;
struct subp1{
double element[7][7];
};
struct subp1 m_subp1={
{ {0.0, 0.00913767235, 0.021840193, 0.025674188, 0.021840193, 0.00913767235, 0.0},
{0.00913767235, 0.025951560, 0.025984481, 0.025984481, 0.025984481, 0.025951560, 0.00913767235},
{0.021840193, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.021840193},
{0.025674188, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.025674188},
{0.021840193, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.025984481, 0.021840193},
{0.00913767235, 0.025951560, 0.025984481, 0.025984481, 0.025984481, 0.025951560, 0.00913767235},
{0.0, 0.00913767235, 0.021840193, 0.025674188, 0.021840193, 0.00913767235, 0.0}}
};
struct result1{
double x;
double y;
int xx;
int yy;
};
CViewCDibView::CViewCDibView()
{
// TODO: add construction code here
}
CViewCDibView::~CViewCDibView()
{
}
BOOL CViewCDibView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CScrollView::PreCreateWindow(cs);
}
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView drawing
void CViewCDibView::OnDraw(CDC* pDC)
{
CViewCDibDoc * pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
//CViewCDibDoc* pDoc=GetDocument();
//ASSERT_VALID(pDoc);
if(!pDoc->m_pDib->IsEmpty())
pDoc->m_pDib->Display(pDC,0,0);
}
void CViewCDibView::OnInitialUpdate()
{
CScrollView::OnInitialUpdate();
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
CSize sizeTotal(pDoc->m_pDib->GetWidth(),pDoc->m_pDib->GetHeight());
// TODO: calculate the total size of this view
//sizeTotal.cx = sizeTotal.cy = 100;
SetScrollSizes(MM_TEXT, sizeTotal);
CMainFrame* pAppFrame=(CMainFrame *)AfxGetApp()->m_pMainWnd;
ASSERT_KINDOF(CMainFrame,pAppFrame);
CRect rect;
pAppFrame->GetClientRect(&rect);
if(rect.Width() >= sizeTotal.cx && rect.Height() >= sizeTotal.cy && (sizeTotal.cx>0||sizeTotal.cy>0))
ResizeParentToFit(FALSE);
}
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView printing
BOOL CViewCDibView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CViewCDibView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CViewCDibView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView diagnostics
#ifdef _DEBUG
void CViewCDibView::AssertValid() const
{
CScrollView::AssertValid();
}
void CViewCDibView::Dump(CDumpContext& dc) const
{
CScrollView::Dump(dc);
}
CViewCDibDoc* CViewCDibView::GetDocument() // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CViewCDibDoc)));
return (CViewCDibDoc*)m_pDocument;
}
#endif //_DEBUG
/////////////////////////////////////////////////////////////////////////////
// CViewCDibView message handlers
LRESULT CViewCDibView::OnRealizePal(WPARAM wParam, LPARAM lParam)
{
ASSERT(wParam != NULL);
CViewCDibDoc* pDoc = GetDocument();
if (pDoc->m_pDib->IsEmpty())
return 0L; // must be a new document
CPalette* pPal = pDoc->m_pDib->GetPalette();
if (pPal != NULL)
{
CWnd* pAppFrame = AfxGetApp()->m_pMainWnd;
CClientDC appDC(pAppFrame);
// All views but one should be a background palette.
// wParam contains a handle to the active view, so the SelectPalette
// bForceBackground flag is FALSE only if wParam == m_hWnd (this view)
CPalette* oldPalette = appDC.SelectPalette(pPal, ((HWND)wParam) != m_hWnd);
if (oldPalette != NULL)
{
UINT nColorsChanged = appDC.RealizePalette();
if (nColorsChanged > 0)
GetDocument()->UpdateAllViews(NULL);
appDC.SelectPalette(oldPalette, TRUE);
}
else
{
TRACE0("\tSelectPalette failed!\n");
}
}
return 0L;
}
void CViewCDibView::OnChangetogray()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
//CDC* pDC;
//pDoc->m_pDib->ChangeToGrayscale(1,0.30,0.59,0.11);
pDoc->m_pDib->ChangeToGrayscale(2,0.30,0.59,0.11);
pDoc->SetModifiedFlag(TRUE);
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
/* HANDLE data1handle;//用来存放图像数据的句柄;
LPBITMAPINFOHEADER lpBi=new BITMAPINFOHEADER;//图像的信息头结构;
HDIB hdib;//用来存放图像数据的句柄
LPBYTE lpBits=new BYTE;//原始图像数据指针
LPBYTE lpRBits=new BYTE;//结果图像数据指针
if(pDoc->m_pDib->m_hDib==NULL)
{
AfxMessageBox("meiyoujubin");
return;
}
hdib=pDoc->m_pDib->m_hDib;
//hdib=GlobalAlloc(GMEM_MOVEABLE,pDoc->m_pDib->GetWidth()*pDoc->m_pDib->GetWidth());
lpBi=(LPBITMAPINFOHEADER)GlobalLock((HGLOBAL)hdib);//获取图像信息头
lpBits=(BYTE*)FindDIBBits((LPBYTE)lpBi); //FindDIBBits是我定义的一个函数、根据图像的结构得到位图的灰度值数据、
//lpBits=(LPBYTE)(lpBi + *(LPDWORD)lpBi + PaletteSize(lpBi));
pDoc->SetModifiedFlag(TRUE); //设置文档修改标志为"真"、为后续的修改存盘作准备;
data1handle=GlobalAlloc(GMEM_MOVEABLE,WIDTHBYTES(lpBi->biWidth*8)*lpBi->biHeight);//声明一个缓冲区用来暂存处理后的图像数据;
lpRBits=(LPBYTE)GlobalLock((HGLOBAL)data1handle);//得到该缓冲区的指针;
AfxGetApp()->BeginWaitCursor();
int i=0,j=0,buf=0;
for(i=lpBi->biHeight;i>=2;i--)
for(j=lpBi->biWidth;j>=2;j--)
{
buf=*(lpBits+(lpBi->biHeight-i)*WIDTHBYTES(lpBi->biWidth*8)+j)-*(lpBits+(lpBi->biHeight-i+1)*WIDTHBYTES(lpBi->biWidth*8)+j-1)+128;
if(buf>255)
buf=255;
if(buf<0)
buf=0;
*(lpRBits+(lpBi->biHeight-i)*WIDTHBYTES(lpBi->biWidth*8)+j)=(BYTE)buf;
}
for( i=0;i<lpBi->biHeight;i++)
for(j=0;j<lpBi->biWidth;j++) //重新写回原始图像的数据缓冲区;
*(lpBits+i*WIDTHBYTES(lpBi->biWidth*8)+j)=*(lpRBits+i*WIDTHBYTES(lpBi->biWidth*8)+j);
AfxGetApp()->EndWaitCursor();
pDoc->m_pDib->m_hDib=hdib;//将处理过的图像数据写回pDoc中的图像缓冲区;
//pDoc->m_pDib->UpdateInternal();//UpdateInternal();
pDoc->m_pDib->BuildPalette();
pDoc->m_pDib->BuildBitmap();
//pDoc->m_pDib->m_pMemDC=pDC->GetSafeHdc();
GlobalUnlock((HGLOBAL)hdib);//解锁、释放缓冲区;
GlobalUnlock((HGLOBAL)data1handle);
GlobalFree((HGLOBAL)hdib);
GlobalFree((HGLOBAL)data1handle);
//pDoc->UpdateAllViews(NULL);
//Invalidate(TRUE);
pDoc->SetModifiedFlag(TRUE);
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
*/
}
void CViewCDibView::OnSobel()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_pDib->EdgeEnhance(10,SOBEL);
//pDoc->m_pDib->Convert2BW(128);
OnRealizePal((WPARAM)m_hWnd,NULL);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnPixeloper()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
WaitCursorBegin();
HDIB NewHDib;
NewHDib=CopyHandle(pDoc->m_pDib->m_hDib);
WORD wBitCount=DIBBitCount(pDoc->m_pDib->m_hDib);
if(wBitCount!=8)
{
WaitCursorEnd();
AfxMessageBox("wBitCount is not 8 level!");
return;
}
int max=256;
LPBITMAPINFO lpSrcDIB=(LPBITMAPINFO)GlobalLock(NewHDib);
if(!lpSrcDIB)
{
WaitCursorEnd();
AfxMessageBox("lpSrcDIB is not right!");
return;
}
LPSTR lpPtr;
LONG thisHeight=DIBHeight(NewHDib);
LONG thisWidth=DIBWidth(NewHDib);
DWORD dwBufferSize=GlobalSize(lpSrcDIB);
int nLineBits=BytesPerLine(lpSrcDIB);
for(int i=0;i<thisHeight;i++)
{
for(int j=0;j<thisWidth;j++)
{
lpPtr=(LPSTR)lpSrcDIB+(dwBufferSize-nLineBits-nLineBits*i)+j;
*lpPtr=max-*lpPtr;
}
}
pDoc->m_pDib->m_hDib=NewHDib;
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
GlobalUnlock(NewHDib);
pDoc->SetModifiedFlag(TRUE);
OnRealizePal((WPARAM)m_hWnd,NULL);
pDoc->UpdateAllViews(NULL);
return;
// pDoc->m_pDib->Reverse();
// pDoc->SetModifiedFlag(TRUE);
// pDoc->UpdateAllViews(NULL);
// return;
}
void CViewCDibView::OnFudiao()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
WaitCursorBegin();
HANDLE data1handle;//用来存放图像数据的句柄;
LPBITMAPINFOHEADER lpBi=new BITMAPINFOHEADER;//图像的信息头结构;
HDIB hdib;//用来存放图像数据的句柄
LPBYTE lpBits=new BYTE;//原始图像数据指针
LPBYTE lpRBits=new BYTE;//结果图像数据指针
if(pDoc->m_pDib->m_hDib==NULL)
{
AfxMessageBox("meiyoujubin");
return;
}
hdib=pDoc->m_pDib->m_hDib;
//hdib=GlobalAlloc(GMEM_MOVEABLE,pDoc->m_pDib->GetWidth()*pDoc->m_pDib->GetWidth());
lpBi=(LPBITMAPINFOHEADER)GlobalLock((HGLOBAL)hdib);//获取图像信息头
lpBits=(BYTE*)FindDIBBits((LPBYTE)lpBi); //FindDIBBits是我定义的一个函数、根据图像的结构得到位图的灰度值数据、
//lpBits=(LPBYTE)(lpBi + *(LPDWORD)lpBi + PaletteSize(lpBi));
pDoc->SetModifiedFlag(TRUE); //设置文档修改标志为"真"、为后续的修改存盘作准备;
data1handle=GlobalAlloc(GMEM_MOVEABLE,WIDTHBYTES(lpBi->biWidth*8)*lpBi->biHeight);//声明一个缓冲区用来暂存处理后的图像数据;
lpRBits=(LPBYTE)GlobalLock((HGLOBAL)data1handle);//得到该缓冲区的指针;
AfxGetApp()->BeginWaitCursor();
int i=0,j=0,buf=0;
for(i=lpBi->biHeight;i>=2;i--)
for(j=lpBi->biWidth;j>=2;j--)
{
buf=*(lpBits+(lpBi->biHeight-i)*WIDTHBYTES(lpBi->biWidth*8)+j)-*(lpBits+(lpBi->biHeight-i+1)*WIDTHBYTES(lpBi->biWidth*8)+j-1)+128;
if(buf>255)
buf=255;
if(buf<0)
buf=0;
*(lpRBits+(lpBi->biHeight-i)*WIDTHBYTES(lpBi->biWidth*8)+j)=(BYTE)buf;
}
for( i=0;i<lpBi->biHeight;i++)
for(j=0;j<lpBi->biWidth;j++) //重新写回原始图像的数据缓冲区;
*(lpBits+i*WIDTHBYTES(lpBi->biWidth*8)+j)=*(lpRBits+i*WIDTHBYTES(lpBi->biWidth*8)+j);
AfxGetApp()->EndWaitCursor();
pDoc->m_pDib->m_hDib=hdib;//将处理过的图像数据写回pDoc中的图像缓冲区;
//pDoc->m_pDib->UpdateInternal();//UpdateInternal();
pDoc->m_pDib->BuildPalette();
pDoc->m_pDib->BuildBitmap();
//pDoc->m_pDib->m_pMemDC=pDC->GetSafeHdc();
GlobalUnlock((HGLOBAL)hdib);//解锁、释放缓冲区;
GlobalUnlock((HGLOBAL)data1handle);
// GlobalFree((HGLOBAL)hdib);
GlobalFree((HGLOBAL)data1handle);
//pDoc->UpdateAllViews(NULL);
//Invalidate(TRUE);
pDoc->SetModifiedFlag(TRUE);
OnRealizePal((WPARAM)m_hWnd,0);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnMymind()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
WaitCursorBegin();
HDIB m_thisDib;
if(pDoc->m_pDib->m_hDib==NULL)
{
WaitCursorEnd();
AfxMessageBox("there is no m_hDib!");
return;
}
m_thisDib=pDoc->m_pDib->m_hDib;
// m_thisDib=CopyHandle(pDoc->m_pDib->m_hDib);//第二次运行这个函数的时候出错了!
int wBitCount=DIBBitCount(m_thisDib);
if(wBitCount!=8)
{
WaitCursorEnd();
return;
}
LPBITMAPINFO lpSource=(LPBITMAPINFO)GlobalLock(m_thisDib);
if(!lpSource)
{
WaitCursorEnd();
return;
}
LONG Height=DIBHeight(lpSource);
LONG Width=DIBWidth(lpSource);
int max=256;
int Bytes=BytesPerLine(lpSource);
LONG buffersize=GlobalSize(m_thisDib);//第二次运行这个函数时这里出错了!
LPSTR lptr;
for(int i=1;i<=Height;i++)
{
for(int j=0;j<Width;j++)
{
lptr=(LPSTR)lpSource+(buffersize-i*Bytes)+j;
*lptr=max-*lptr;
}
}
AfxGetApp()->EndWaitCursor();
// ::memcpy((LPBYTE)GlobalLock(pDoc->m_pDib->m_hDib),(LPBYTE)GlobalLock(m_thisDib),GlobalSize(m_thisDib));
pDoc->m_pDib->m_hDib=m_thisDib;
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
GlobalUnlock(m_thisDib);
// GlobalUnlock(pDoc->m_pDib->m_hDib);
OnRealizePal((WPARAM)m_hWnd,NULL);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnDosobel()
{
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
// TODO: Add your command handler code here
// if(m_pthreshold.DoModal()==IDOK)
// int p=m_pthreshold.m_threshold;
pDoc->m_pDib->EdgeEnhance(10,16);
if(pDoc->m_pDib->IsEmpty())
return;
double p;
thisparam* m_newthisparam=new thisparam;
m_newthisparam=&m_thisparam;
if(m_newthisparam->DoModal()==IDOK)
p=m_newthisparam->m_thisparamnew;
int thr=pDoc->m_pDib->GetThreshold(p);
pDoc->m_pDib->Convert2BW(thr);
pDoc->m_pDib->BuildBitmap();
pDoc->m_pDib->BuildPalette();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnEdgedetect()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
/* CDib m_mdib1;
CDib m_mdib2;
m_mdib1.m_hDib=CopyHandle(pDoc->m_pDib->m_hDib);
m_mdib2.m_hDib=CopyHandle(pDoc->m_pDib->m_hDib);
m_mdib1.Contour(TRUE);
m_mdib2.Contour(FALSE);
LONG height=DIBHeight(pDoc->m_pDib->m_hDib);
LONG width=DIBWidth(pDoc->m_pDib->m_hDib);
LPBITMAPINFO lpSrc1=(LPBITMAPINFO)GlobalLock(m_mdib1.m_hDib);
LPBITMAPINFO lpSrc2=(LPBITMAPINFO)GlobalLock(m_mdib2.m_hDib);
LONG buffersize=GlobalSize(lpSrc1);
LPSTR lpPtr1;
LPSTR lpPtr2;
int Bytes=BytesPerLine(lpSrc1);
for(int i=1;i<=height;i++)
for(int j=1;j<=width;j++)
{
lpPtr1=(LPSTR)lpSrc1+(buffersize-i*Bytes)+j;
lpPtr2=(LPSTR)lpSrc2+(buffersize-i*Bytes)+j;
if(*lpPtr1<=*lpPtr2)
*lpPtr1=*lpPtr2;
}
pDoc->m_pDib->m_hDib=m_mdib1.m_hDib;
pDoc->m_pDib->BuildPalette();
pDoc->m_pDib->BuildBitmap();
GlobalUnlock(m_mdib1.m_hDib);
GlobalUnlock(m_mdib2.m_hDib);
// HDIB m_dib1;
// HDIB m_dib2;
// m_dib1=CopyHandle(pDoc->m_pDib->m_hDib);
// m_dib2=CopyHandle(pDoc->m_pDib->m_hDib);
*/// pDoc->m_pDib->Contour(TRUE);
// pDoc->m_pDib->Contour(FALSE);
pDoc->m_pDib->Edge();
pDoc->m_pDib->BuildBitmap();//>UpdateInternal();
pDoc->m_pDib->BuildPalette();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnMediafilter()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
if(!pDoc->m_pDib->MedianFilter())
{
AfxMessageBox("the media filter is false");
return;
}
pDoc->UpdateAllViews(NULL);
pDoc->SetModifiedFlag(TRUE);
}
void CViewCDibView::OnOpen()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_pDib->MorphOpen(TRUE);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnClose()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
pDoc->m_pDib->MorphClose(TRUE);
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
void CViewCDibView::OnBw()
{
// TODO: Add your command handler code here
CViewCDibDoc* pDoc=GetDocument();
ASSERT_VALID(pDoc);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -