📄 thinning.cpp
字号:
#include "stdafx.h"
#include "Thinning.h"
#include "cdib.h"
#define WIDTHBYTES(i) ((i+31)/32*4)
CThinning::CThinning(void)
{
}
CThinning::~CThinning(void)
{
}
//refresh the window
void CThinning::MicroThin(HWND hWnd,CAbrasionMeasurementDoc *pdc)
{
CDib * pDib1 = pdc->m_pDibInit;
if(Thinning(hWnd,pdc))
InvalidateRect(hWnd,NULL,TRUE);
else
MessageBox(hWnd,"细化失败!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
}
BOOL CThinning::Thinning(HWND hWnd,CAbrasionMeasurementDoc *pdc)
{
CAbrasionMeasurementDoc * pDoc = pdc;
CDib * pDib = pDoc->m_pDibInit;
LPBITMAPINFOHEADER lpBMIH=pDib->m_lpBMIH;
LPBITMAPINFOHEADER bi=pDib->m_lpBMIH;
int NumColors=256;
// 判断是否是8-bpp位图
if (lpBMIH->biBitCount != 8)
{
if(bi->biClrUsed!=0)
NumColors=(DWORD)bi->biClrUsed;
else
switch(bi->biBitCount)
{
case 1:
NumColors=2;
break;
case 4:
NumColors=16;
break;
case 8:
NumColors=256;
break;
case 24:
NumColors=0;
break;
default: // 提示用户
MessageBox(hWnd,"目前只支持256色位图的图象分割!", "系统提示" ,
MB_ICONINFORMATION | MB_OK);
// 返回
return true;
}
}
DWORD BufSize;
LPBITMAPINFOHEADER lpImgData=lpBMIH;
LPSTR lpPtr;
HLOCAL hTempImgData;
LPBITMAPINFOHEADER lpTempImgData;
HBITMAP hBitmap=pDib->m_hBitmap;
BITMAPFILEHEADER bf=pDib->GetBITMAPFILEHEADER();
/*
CFile cf;
int aaa=sizeof(BITMAPFILEHEADER);
try
{
if(cf.Open((LPSTR)&bf,(CFile::modeNoTruncate|CFile::modeCreate|CFile::modeWrite)))
{
cf.SeekToBegin();
int bbb=cf.Read((LPSTR)&bf, aaa);
if(int i=bbb)
return (FALSE);
}
}
catch(CFileException *e)
{
return (FALSE);
}
*/
DWORD LineBytes=(DWORD)WIDTHBYTES(bi->biWidth*bi->biBitCount);
LPSTR lpTempPtr;
HDC hDc;
HFILE hf;
LONG x,y;
int num;
BOOL Finished;
int nw,n,ne,w,e,sw,s,se;
static int erasetable[256]={
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
1,1,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
1,1,0,1,1,1,0,1,
0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,1,
0,0,1,1,0,0,1,1,
1,1,0,1,1,1,0,1,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,1,1,
0,0,0,0,0,0,0,0,
1,1,0,0,1,1,0,0,
1,1,0,1,1,1,0,0,
1,1,0,0,1,1,1,0,
1,1,0,0,1,0,0,0
};
if( lpBMIH->biBitCount != 8){
MessageBox(hWnd,"Must be a mono bitmap with grayscale palette!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
BufSize=bf.bfSize-sizeof(BITMAPFILEHEADER);
if((hTempImgData=LocalAlloc(LHND,BufSize))==NULL)
{
MessageBox(hWnd,"Error alloc memory!","Error Message",MB_OK|MB_ICONEXCLAMATION);
return FALSE;
}
lpTempImgData=(LPBITMAPINFOHEADER)LocalLock(hTempImgData);
//copy image data
memcpy(lpTempImgData,lpImgData,BufSize);
Finished=FALSE;
while(!Finished){
Finished=TRUE;
for (y=1;y<bi->biHeight-1;y++){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
x=1;
while(x<bi->biWidth-1){
if(*(lpPtr+x)==0){
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
if( (w==255)|| (e==255)){
nw=(unsigned char)*(lpPtr+x+LineBytes-1);
n=(unsigned char)*(lpPtr+x+LineBytes);
ne=(unsigned char)*(lpPtr+x+LineBytes+1);
sw=(unsigned char)*(lpPtr+x-LineBytes-1);
s=(unsigned char)*(lpPtr+x-LineBytes);
se=(unsigned char)*(lpPtr+x-LineBytes+1);
num=nw/255+n/255*2+ne/255*4+w/255*8+e/255*16+sw/255*32+s/255*64+se/255*128;
if(erasetable[num]==1){
*(lpPtr+x)=(BYTE)255;
*(lpTempPtr+x)=(BYTE)255;
Finished=FALSE;
x++;
}
}
}
x++;
}
}
for (x=1;x<bi->biWidth-1;x++){
y=1;
while(y<bi->biHeight-1){
lpPtr=(char *)lpImgData+(BufSize-LineBytes-y*LineBytes);
lpTempPtr=(char *)lpTempImgData+(BufSize-LineBytes-y*LineBytes);
if(*(lpPtr+x)==0){
n=(unsigned char)*(lpPtr+x+LineBytes);
s=(unsigned char)*(lpPtr+x-LineBytes);
if( (n==255)|| (s==255)){
nw=(unsigned char)*(lpPtr+x+LineBytes-1);
ne=(unsigned char)*(lpPtr+x+LineBytes+1);
w=(unsigned char)*(lpPtr+x-1);
e=(unsigned char)*(lpPtr+x+1);
sw=(unsigned char)*(lpPtr+x-LineBytes-1);
se=(unsigned char)*(lpPtr+x-LineBytes+1);
num=nw/255+n/255*2+ne/255*4+w/255*8+e/255*16+sw/255*32+s/255*64+se/255*128;
if(erasetable[num]==1){
*(lpPtr+x)=(BYTE)255;
*(lpTempPtr+x)=(BYTE)255;
Finished=FALSE;
y++;
}
}
}
y++;
}
}
}
if(hBitmap!=NULL)
DeleteObject(hBitmap);
hDc=GetDC(hWnd);
hBitmap=CreateDIBitmap(hDc, (LPBITMAPINFOHEADER)lpTempImgData, (LONG)CBM_INIT,
(LPSTR)lpTempImgData+sizeof(BITMAPINFOHEADER) +NumColors*sizeof(RGBQUAD),
(LPBITMAPINFO)lpTempImgData, DIB_RGB_COLORS);
hf=_lcreat("c:\\thinning.bmp",0);
_lwrite(hf,(LPSTR)&bf,sizeof(BITMAPFILEHEADER));
_lwrite(hf,(LPSTR)lpTempImgData,BufSize);
_lclose(hf);
ReleaseDC(hWnd,hDc);
LocalUnlock(hTempImgData);
LocalFree(hTempImgData);
return TRUE;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -