📄 hwpreview.cpp
字号:
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB());
//HDIB hDibSource ,hDibDest;
LONG m_lCount[256];
// DIB的高度
LONG m_lHeight;
// DIB的宽度
LONG m_lWidth;
LONG m_lLineBytes;
int i,j;
LPSTR lpdibS,lpdibD,lpBitsS,lpBitsD;
LPBYTE lpSrcS,lpSrcD;
BeginWaitCursor();
hDibSource = pDoc->GetHDIB();
hDibDest = pDoc->GetHDIB2();
lpdibS = (LPSTR) ::GlobalLock((HGLOBAL)hDibSource);
lpdibD = (LPSTR) ::GlobalLock((HGLOBAL)hDibDest);
lpBitsS = ::FindDIBBits(lpdibS);
lpBitsD = ::FindDIBBits(lpdibD);
m_lWidth = ::DIBWidth(lpdibS);
m_lHeight =::DIBHeight(lpdibS);
m_lLineBytes = WIDTHBYTES( m_lWidth*8 );
for (i=0;i<256;i++) {
m_lCount[i]=0;
}
for (i=0;i<m_lHeight;i++) {
for (j=0;j<m_lWidth;j++) {
lpSrcS =(LPBYTE) lpBitsS + i * m_lLineBytes +j;
m_lCount[*lpSrcS]++;
}
}
// 灰度映射表
BYTE bMap[256];
// 临时变量
LONG lTemp;
for (i=0 ;i<256;i++) {
lTemp = 0;
for (j=0; j<=i;j++) {
lTemp += m_lCount[j];
}
bMap[i]=(BYTE) ((double)lTemp * 255/m_lWidth/m_lHeight);
(BYTE) (lTemp * 255/m_lWidth/m_lHeight);
}
for (i=0;i<m_lHeight;i++) {
for (j=0;j<m_lWidth;j++) {
lpSrcS =(LPBYTE) lpBitsS + i * m_lLineBytes +j;
lpSrcD =(LPBYTE) lpBitsD + i * m_lLineBytes +j;
*lpSrcD = bMap[*lpSrcS];
}
}
::GlobalUnlock((HGLOBAL)hDibSource);
::GlobalUnlock((HGLOBAL)hDibDest);
EndWaitCursor();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
*/
}
//Hough变换进行倾斜校正
void CHwpreView::OnEdgeHough()
{
// TODO: Add your command handler code here
//Hough运算
// 获取文档
CHwpreDoc * pDoc = GetDocument();
// 指向DIB的指针
LPSTR lpDIB;
// 指向DIB象素指针
LPSTR lpDIBBits;
// 锁定DIB
lpDIB = (LPSTR) ::GlobalLock((HGLOBAL) pDoc->GetHDIB2());
// 判断是否是8-bpp位图(这里为了方便,只处理8-bpp位图的Hough变换,其它的可以类推)
if (::DIBNumColors(lpDIB) != 256)
{
// 提示用户
MessageBox("目前只支持256色位图的运算!", "系统提示" , MB_ICONINFORMATION | MB_OK);
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
// 返回
return;
}
// 更改光标形状
BeginWaitCursor();
// 找到DIB图像象素起始位置
lpDIBBits = ::FindDIBBits(lpDIB);
// 调用HoughDIB()函数对DIB
double lAngleRotate,lAngleRotate2;
if (HoughDIB2( lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB),-10,10,0.1, 0,::DIBWidth(lpDIB)*4/5,::DIBHeight(lpDIB),::DIBWidth(lpDIB), lAngleRotate)
&& HoughDIB2( lpDIBBits, ::DIBWidth(lpDIB), ::DIBHeight(lpDIB),-10,10,0.1,0,0,::DIBHeight(lpDIB),::DIBWidth(lpDIB)/5,lAngleRotate2))
{
CString str;
str.Format("%f,%f",lAngleRotate,lAngleRotate2);
((CMainFrame*)AfxGetMainWnd())->SetMessageText(str);
HDIB hNewDIB=NULL;
// 调用RotateDIB()函数旋转DIB RotateDIB2双线性插值
hNewDIB=(HDIB) RotateDIB2(lpDIB,-(lAngleRotate+lAngleRotate2)/2);
if (hNewDIB!=NULL) {
pDoc->ReplaceHDIB2(hNewDIB);
pDoc->InitDIBData2();
pDoc->SetModifiedFlag(TRUE);
pDoc->UpdateAllViews(NULL);
}
else{
// 提示用户
MessageBox("分配内存失败!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
}
else
{
// 提示用户
MessageBox("分配内存失败或者图像中含有0和255之外的像素值!", "系统提示" , MB_ICONINFORMATION | MB_OK);
}
// 解除锁定
::GlobalUnlock((HGLOBAL) pDoc->GetHDIB2());
// 恢复光标
EndWaitCursor();
}
void CHwpreView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
if (m_MouseRect.PtInRect(point)) {
m_iIsDraging =1;
m_DragRect.left =m_DragRect.right = point.x ;
m_DragRect.top = m_DragRect.bottom = point.y;
::SetCursor(::LoadCursor(NULL,IDC_CROSS));
}
CScrollView::OnLButtonDown(nFlags, point);
}
void CHwpreView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
//m_DragRect.right = point.x;
//m_DragRect.bottom= point.y;
m_DragRect.NormalizeRect();
CRect * cr,crTemp; int i=0;POSITION pst;
m_DragRect = DispToDib(m_DragRect);
if (m_DragRect.Width()<5 && m_DragRect.Height()<5) {
m_iIsDraging =0;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
cr = (CRect *) m_ListRect.GetAt(pst);
if (cr->PtInRect(CPoint(m_DragRect.left,m_DragRect.top )) ) {
m_ptCurRect= CPoint(cr->left ,cr->top );
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
}else if (0!=m_iIsDraging) {
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
cr = (CRect *) m_ListRect.GetAt(pst);
if (crTemp.IntersectRect(cr,m_DragRect )) {
m_ListRect.RemoveAt(pst);
delete cr;
pst=m_ListRect.GetHeadPosition();
i=-1;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
m_iIsDraging =0;
CRect * cnewrc = new CRect(m_DragRect);
m_ListRect.AddTail(cnewrc);
m_ptCurRect= CPoint(cnewrc->left ,cnewrc->top );
}
GetDocument()->UpdateAllViews(NULL);
CScrollView::OnLButtonUp(nFlags, point);
}
void CHwpreView::OnMouseMove(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
TRACE("mouserc : %d,%d,%d,%d \n",m_MouseRect.left ,m_MouseRect.top,m_MouseRect.right,m_MouseRect.bottom);
TRACE2("x= %d,y=%d \n",point.x,point.y);
CString str;
str.Format("x= %d,y=%d \n",point.x,point.y);
((CMainFrame*)AfxGetMainWnd())->SetMessageText(str);
if (m_MouseRect.PtInRect(point)) {
if (0 != m_iIsDraging) {
m_DragRect.right = point.x;
m_DragRect.bottom = point.y;
GetDocument()->SetModifiedFlag(TRUE);
//GetDocument()->UpdateAllViews(NULL);
InvalidateRect(m_DragRect+ CRect(0,0,1,1),TRUE);
::SetCursor(::LoadCursor(NULL,IDC_CROSS));
}else{
//::SetCursor(::LoadCursor(NULL,IDC_SIZEWE));
}
}
CScrollView::OnMouseMove(nFlags, point);
}
void CHwpreView::OnLButtonDblClk(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CScrollView::OnLButtonDblClk(nFlags, point);
}
CRect CHwpreView::DibToDisp(CRect & cr)
{
CRect ct ;
CSize sz = GetDocument()->GetDocSize();
ct.left = (int) ((double) cr.left * m_MouseRect.Width() /sz.cx +m_MouseRect.left );
ct.right =(int) ((double) cr.right * m_MouseRect.Width() /sz.cx +m_MouseRect.left );
ct.top = (int) ((double) cr.top * m_MouseRect.Height() /sz.cy +m_MouseRect.top );
ct.bottom=(int) ((double) cr.bottom* m_MouseRect.Height() /sz.cy +m_MouseRect.top );
return ct;
}
CRect CHwpreView::DispToDib(CRect & cr)
{
CRect ct ;
CSize sz = GetDocument()->GetDocSize();
ct.left = (int) ((double) (cr.left-m_MouseRect.left) * sz.cx / m_MouseRect.Width() );
ct.right =(int) ((double) (cr.right-m_MouseRect.left) * sz.cx / m_MouseRect.Width() );
ct.top = (int) ((double) (cr.top-m_MouseRect.top ) * sz.cy / m_MouseRect.Height() );
ct.bottom=(int) ((double) (cr.bottom-m_MouseRect.top ) * sz.cy / m_MouseRect.Height() );
return ct;
}
//水平投影
void CHwpreView::OnEgHproject()
{
// TODO: Add your command handler code here
CRect cr;int i = 0 ; POSITION pst;BOOL isfind=FALSE;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
cr = *((CRect * ) m_ListRect.GetAt(pst));
if (cr.PtInRect(m_ptCurRect)) {
isfind =TRUE;
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
if (!isfind) {
cr.left = cr.top = 0;cr.right = GetDocument()->GetDocSize2().cx -1;
cr.bottom = GetDocument()->GetDocSize2().cy -1;
}
int *lcount = new int[cr.Height()+1];
::HprojectDIB2((LPSTR)GetDocument()->GetHDIB2(),lcount,cr.left ,cr.top ,cr.right ,cr.bottom );
//平滑
//::Smoth(lcount,cr.Height()+1,3);
//GetDocument()->HDIBCOPYTOHDIB2();
HDIB hdibD = GetDocument()->GetHDIB2();
LPSTR hlpstr = (LPSTR) ::GlobalLock((HGLOBAL) hdibD);
LPSTR hBits = ::FindDIBBits(hlpstr);
LPBYTE hbitsD ;
LONG lHeight = ::DIBHeight(hlpstr);
LONG lWidth = ::DIBWidth(hlpstr);
LONG m_lLineBytes = WIDTHBYTES( lWidth*8 );
memset(hBits,(BYTE)255, lHeight * m_lLineBytes);
int j ;
for (i= cr.top;i<=cr.bottom;i++) {
for (j= 1;j<=lcount[i-cr.top];j++) {
hbitsD = (LPBYTE ) hBits + m_lLineBytes * (lHeight -1 -i) + j+cr.left;
* hbitsD = 0;
}
}
delete[] lcount;
::GlobalUnlock((HGLOBAL) hdibD);
GetDocument()->UpdateAllViews(NULL);
}
//垂直投影
void CHwpreView::OnEgVproject()
{
// TODO: Add your command handler code here
CRect cr;int i = 0 ; POSITION pst;BOOL isfind=FALSE;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
cr = *((CRect * ) m_ListRect.GetAt(pst));
if (cr.PtInRect(m_ptCurRect)) {
isfind =TRUE;
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
if (!isfind) {
cr.left = cr.top = 0;cr.right = GetDocument()->GetDocSize2().cx -1;
cr.bottom = GetDocument()->GetDocSize2().cy -1;
}
int *lcount = new int[cr.Width()+1];
::VprojectDIB2((LPSTR)GetDocument()->GetHDIB2(),lcount,cr.left ,cr.top ,cr.right ,cr.bottom );
//GetDocument()->HDIBCOPYTOHDIB2();
HDIB hdibD = GetDocument()->GetHDIB2();
LPSTR hlpstr = (LPSTR) ::GlobalLock((HGLOBAL) hdibD);
LPSTR hBits = ::FindDIBBits(hlpstr);
LPBYTE hbitsD ;
LONG lHeight = ::DIBHeight(hlpstr);
LONG lWidth = ::DIBWidth(hlpstr);
LONG m_lLineBytes = WIDTHBYTES( lWidth*8 );
memset(hBits,(BYTE)255, lHeight * m_lLineBytes);
int j ;
for (i= cr.left;i<=cr.right;i++) {
for (j= 1;j<=lcount[i-cr.left];j++) {
hbitsD = (LPBYTE)hBits + m_lLineBytes * (lHeight -1 +j - cr.bottom) +i ;
* hbitsD = 0;
}
}
delete[] lcount;
::GlobalUnlock((HGLOBAL) hdibD);
GetDocument()->UpdateAllViews(NULL);
}
void CHwpreView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
{
// TODO: Add your message handler code here and/or call default
CRect * cr;int i = 0 ; POSITION pst;
switch(nChar) {
case VK_DELETE:
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
cr = ((CRect * ) m_ListRect.GetAt(pst));
if (cr->PtInRect(m_ptCurRect)) {
m_ListRect.RemoveAt(pst);
delete cr;
InvalidateRect(m_MouseRect,TRUE);
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
break;
default:
;
}
CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
}
void CHwpreView::OnEgHline()
{
// TODO: Add your command handler code here
// TODO: Add your command handler code here
CRect cr,*pcr;int i = 0 ; POSITION pst;BOOL isfind=FALSE;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
pcr = ((CRect * ) m_ListRect.GetAt(pst));
cr = *pcr;
if (cr.PtInRect(m_ptCurRect)) {
m_ListRect.RemoveAt(pst);
delete pcr;
isfind =TRUE;
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
if (!isfind) {
cr.left = cr.top = 0;cr.right = GetDocument()->GetDocSize().cx -1;
cr.bottom = GetDocument()->GetDocSize().cy -1;
}
int *lcount = new int[cr.Height()+1];
::HprojectDIB2((LPSTR)GetDocument()->GetHDIB(),lcount,cr.left ,cr.top ,cr.right ,cr.bottom );
CDlgLine dlgline;
dlgline.m_uintLineHeight = 80;
dlgline.m_uintLineVal = 10;
if (dlgline.DoModal()!=IDOK) {
return;
}
int iLineHeight = dlgline.m_uintLineHeight;
int iLineVal = dlgline.m_uintLineVal;
int iCurLineBegin = -1 ;
BOOL isfindingLineBegin =TRUE;
CPtrList pregl;
i= 0;
while (i<=cr.Height())
{
if (lcount[i]>iLineVal) {
if (isfindingLineBegin) {
iCurLineBegin=i;
isfindingLineBegin=FALSE;
}else{
;
}
}else{
if (isfindingLineBegin) {
;
}else{
m_ListRect.AddTail(new CRect(cr.left,iCurLineBegin+cr.top ,cr.right,i-1+cr.top));
isfindingLineBegin=TRUE;
}
}
i++;
}
GetDocument()->HDIBCOPYTOHDIB2();
HDIB hdibD = GetDocument()->GetHDIB2();
LPSTR hlpstr = (LPSTR) ::GlobalLock((HGLOBAL) hdibD);
LPSTR hBits = ::FindDIBBits(hlpstr);
LPBYTE hbitsD ;
LONG lHeight = ::DIBHeight(hlpstr);
LONG lWidth = ::DIBWidth(hlpstr);
LONG m_lLineBytes = WIDTHBYTES( lWidth*8 );
memset(hBits,(BYTE)255, lHeight * lWidth);
int j ;
for (i= cr.top;i<=cr.bottom;i++) {
for (j= 1;j<=lcount[i-cr.top];j++) {
hbitsD = (LPBYTE ) hBits + m_lLineBytes * (lHeight -1 -i) + j+cr.left;
* hbitsD = 0;
}
}
delete[] lcount;
::GlobalUnlock((HGLOBAL) hdibD);
}
void CHwpreView::OnEdHega2()
{
// TODO: Add your command handler code here
CRect cr,*pcr;int i = 0 ; POSITION pst;BOOL isfind=FALSE;
if (!m_ListRect.IsEmpty()) {
pst = m_ListRect.GetHeadPosition();
do {
pcr = ((CRect * ) m_ListRect.GetAt(pst));
cr = *pcr;
if (cr.PtInRect(m_ptCurRect)) {
m_ListRect.RemoveAt(pst);
delete pcr;
isfind =TRUE;
break;
}else{
m_ListRect.GetNext(pst);
}
i++;
} while(i<m_ListRect.GetCount());
}
if (!isfind) {
cr.left = cr.top = 0;cr.right = GetDocument()->GetDocSize().cx -1;
cr.bottom = GetDocument()->GetDocSize().cy -1;
}
int *lcount = new int[cr.Height()+1];
::HprojectDIB2((LPSTR)GetDocument()->GetHDIB(),lcount,cr.left ,cr.top ,cr.right ,cr.bottom );
//平滑
::Smoth(lcount,cr.Height()+1,3);
::Smoth(lcount,cr.Height()+1,3);
CDlgLine dlgline;
dlgline.m_uintLineHeight = 20;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -